开发者

add an element AFTER another?

when i use append it add an element WITHIN an element. i want to add an element AFTER an element.

eg.

 <div class="link">
       <input class="link" type="text" value="Skriv länk" />
       <br />
 </div>
开发者_如何学编程

how do i add an element after 'input' but before 'br'?


$("input").after("<p>Hello</p>");

After on jQuery doc: http://api.jquery.com/after/

You also have the insertAfter:

$("<p>Hello</p>").insertAfter("input");

Will work just fine.


Use insertBefore() or insertAfter().


You should really add an id attribute to your input not only for performance but to be sure that you append only after that specific input:

<div class="link">
       <input id="textBox" class="link" type="text" value="Skriv länk" />
       <br />
 </div>

if using jquery

$("#textBox").after("<p>Hello</p>");

else

var textBox = document.getElementById("textBox");
var element = document.createElement("<p>Hello</p>");
textBox.parentNode.insertBefore(element, textBox.nextSibling);


// create your element
var $el = $("<div>");

// append after input
$("input.link").after( $el );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜