jquery-append

The preferred way of creating a new element with jQuery

北城余情 提交于 2019-11-26 04:04:56
问题 I\'ve got 2 ways I can create a <div> using jQuery . Either: var div = $(\"<div></div>\"); $(\"#box\").append(div); Or: $(\"#box\").append(\"<div></div>\"); What are the drawbacks of using second way other than re-usability? 回答1: The first option gives you more flexibilty: var $div = $("<div>", {id: "foo", "class": "a"}); $div.click(function(){ /* ... */ }); $("#box").append($div); And of course .html('*') overrides the content while .append('*') doesn't, but I guess, this wasn't your

How to add a list item to an existing unordered list?

╄→гoц情女王★ 提交于 2019-11-26 01:46:05
问题 I have code that looks like this: <div id=\"header\"> <ul class=\"tabs\"> <li><a href=\"/user/view\"><span class=\"tab\">Profile</span></a></li> <li><a href=\"/user/edit\"><span class=\"tab\">Edit</span></a></li> </ul> </div> I\'d like to use jQuery to add the following to the list: <li><a href=\"/user/messages\"><span class=\"tab\">Message Center</span></a></li> I tried this: $(\"#content ul li:last\").append(\"<li><a href=\"/user/messages\"><span class=\"tab\">Message Center</span></a></li>

Creating a div element in jQuery [duplicate]

前提是你 提交于 2019-11-25 22:05:57
问题 This question already has answers here : jQuery document.createElement equivalent? (13 answers) Closed last year . How do I create a div element in jQuery ? 回答1: You can use append (to add at last position of parent) or prepend (to add at fist position of parent): $('#parent').append('<div>hello</div>'); // or $('<div>hello</div>').appendTo('#parent'); Alternatively, you can use the .html() or .add() as mentioned in a different answer. 回答2: As of jQuery 1.4 you can pass attributes to a self