开发者

JQUERY, TEXT, appending an LI to a list with a SPAN wrapped?

here is my current line of code:

$("<li>").te开发者_JS百科xt($(this).text()).appendTo("#theList");

Which results in

<ul id="theList">
<li>blah</li>
<li>blah</li>
</ul>

How can I update that code above to result in:

<ul id="theList">
<li><span>blah</span></li>
<li><span>blah</span></li>
</ul>


There are two ways of doing this. I know from your previous question you are getting this data from a span in which case you can clone() it:

$("span.findme").clone().appendTo("#theList");

You might want to remove the class:

$("span.findme").clone().removeClass("findme").appendTo("#theList");

Alternatively using each():

$("span.findme").each(function() {
  var span = $("<span>").text($(this).text());
  $("<li>").append(span).appendTo("#theList");
});


you can probably use wrap:

$("<li>").text($(this).text()).wrap("<span></span>").appendTo("#theList");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜