jQuery: Add element after another element

2019-01-06 09:18发布

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.

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>

4条回答
来,给爷笑一个
2楼-- · 2019-01-06 09:23

Solved jQuery: Add element after another element

<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>
查看更多
祖国的老花朵
3楼-- · 2019-01-06 09:47

try using the after() method:

$('#bla').after('<div id="space"></div>');

Documentation

查看更多
叛逆
4楼-- · 2019-01-06 09:47

try

.insertAfter()

here

$(content).insertAfter('#bla');
查看更多
贪生不怕死
5楼-- · 2019-01-06 09:47

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 ).

Second thing, you need the after(), not append() function.

查看更多
登录 后发表回答