开发者

how i can append li to ul in jQuery?

开发者 https://www.devze.com 2023-02-28 23:07 出处:网络
i know it\'s stupid question but someone can answer me i have error div > ul > li i want to add new li with this text. how i can do that

i know it's stupid question but someone can answer me

i have error div > ul > li

i want to add new li with this text. how i can do that

 $("#error.error ul").append('<li/>','you need to say blah first');

what i do wrong and what other method i need to write for doing this. when i write this code exist one li delete a开发者_Python百科nd this text add to ul


 $("#error.error ul").append('<li>you need to say blah first</li>');


Your code...

$("#error.error ul").append('<li/>','you need to say blah first');

Your query...

What [did] I do wrong[?]

You pass the entire piece of HTML serialised, you don't pass the text node as the second argument.

The code you want is...

$("#error.error ul").append('<li>you need to say blah first</li>');

...or...

$('<li>you need to say blah first</li>').appendTo("#error.error ul")

Also note that you most probably can remove the .error in your selector string as an id should be unique and only refer to one element.


$("#error.error ul").append($('<li/>').text('you need to say blah first'));


Try this one:

$("<li/>").appendTo("#error.error ul").html("you need to say blah first");

CLICK HERE TO SEE THE DEMO


I think this will work :)

var listElement = $('<li></li>');
listElement.text('Demo #3').appendTo($('ul#error.error'));

Click here to see the demo

0

精彩评论

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