jQuery 添加旁边(添加后)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2607592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:52:45  来源:igfitidea点击:

jQuery add next to (add after)

jquery

提问by Qiao

HTML is
<a>ref</a>

HTML是
<a>ref</a>

I need to get
<a>ref</a>text

我需要得到
<a>ref</a>text

How can i do this? $('a').append('text')only insert text into <a></a>, not after it

我怎样才能做到这一点?$('a').append('text')只在其中插入文本<a></a>,而不是在它之后

回答by Jeff Sternal

Use afteror insertAfter:

使用afterinsertAfter

$('a').after('text');
$('text').insertAfter('a');

回答by rahul

$('a').after("text");

回答by Deva.TamilanbanThevar

Using the following HTML:

使用以下 HTML:

<div class="container">
  <h2>Greetings</h2>
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>

Content can be created and then inserted after several elements at once:

可以创建内容,然后一次插入多个元素之后:

$('.inner').after('<p>Test</p>');

Each inner element gets this new content:

每个内部元素都获得这个新内容:

<div class="container">
  <h2>Greetings</h2>
  <div class="inner">Hello</div>
  <p>Test</p>
  <div class="inner">Goodbye</div>
  <p>Test</p>
</div>

An element in the DOM can also be selected and inserted after another element:

DOM 中的一个元素也可以被选中并插入到另一个元素之后:

$('.container').after($('h2'));

If an element selected this way is inserted elsewhere, it will be moved rather than cloned:

如果以这种方式选择的元素被插入到别处,它将被移动而不是被克隆:

<div class="container">
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>
<h2>Greetings</h2>

回答by jAndy

use .insertAfter()or .after()

使用.insertAfter().after()

回答by Rakesh Kumar

You can do something like this:

你可以这样做:

$('').insertAfter('class name or content string') ;