jQuery 如何在另一个元素之后添加一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2244605/
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
How can I add an element after another element?
提问by skerit
I have a certain textbox and I want to add a div after it.
I've tried the .append()
function, but that only adds the div in the element.
我有一个特定的文本框,我想在它后面添加一个 div。我已经尝试过该.append()
功能,但这只会在元素中添加 div。
For example, I have:
例如,我有:
<input type="text" id="bla" />
and I want to change that into:
我想把它改成:
<input type="text" id="bla" /><div id="space"></div>
回答by Rowan
try using the after()
method:
尝试使用以下after()
方法:
$('#bla').after('<div id="space"></div>');
回答by Rifat
try
尝试
.insertAfter()
here
这里
$(content).insertAfter('#bla');
回答by naivists
First of all, input
element shouldn't have a closing tag (from http://www.w3.org/TR/html401/interact/forms.html#edef-INPUT: End tag: forbidden
).
首先,input
元素不应该有结束标签(来自http://www.w3.org/TR/html401/interact/forms.html#edef-INPUT: End tag: forbidden )。
Second thing, you need the after()
, not append()
function.
第二件事,你需要after()
, 而不是append()
函数。
回答by Ashar Zafar
Solved jQuery: Add element after another element
解决了 jQuery:在另一个元素之后添加元素
<script>
$( "p" ).append( "<strong>Hello</strong>" );
</script>
OR
或者
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery ( ".sidebar_cart" ) .append( "<a href='http://#'>Continue Shopping</a>" );
});
</script>