开发者

jQuery wrap each list in a ul tag

Without going into details, I have the following jQuery code:

$("nav ul ul").each(function () {
    var navitems = $(this).html();
    alert(navitems);
});

I knopw .html uses InnerHTML so the alerts are all

<li>xxxxx</li>
<li>xxxxx</li>

But how can edit the jQuery so the alerts are like

<ul>
<li>xxxxx</li&开发者_开发百科gt;
<li>xxxxx</li>
</ul>


$("nav ul ul").each(function () {
        var navitems = $(this).html();
        var tag = $(this).get(0).nodeName.toLowerCase();
        alert('<' + tag + '>' + navitems + '</' + tag + '>');
});


i would do something like this:

$("nav ul ul").each(function () {
   alert( $('<div>').append($(this).clone()).remove().html() );
 }); 

it creates a dummy div, appends the selected UL in it and removes the dummy and gives you back its html content.

here is a demo: http://jsfiddle.net/meo/QmTSF/


Do you mean something like this:

$("nav ul ul").each(function () {
    var navitems = "<ul>" + $(this).html() + "</ul>";
    alert(navitems);
});


i don't really know what exactly you want. but as I see it, the parent() selector could help.

http://api.jquery.com/parent/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜