检索对象自己的标记



我正在使用jQuery对li标签列表进行排序,我当前的代码是:

var arr = [];
$("ul li").each(function() {
    arr.push($(this));
});
arr.sort(cmpFunction);
$("ul").find("li").remove();
$.each(arr, function(index, item){
    console.log(item.html());
});

不过,我在console.log中发现的是,我正在丢失包含外部的li标签(带有我想保留的html5数据属性)

有没有其他选项.html()将给我对象li标记以及

我在这里提供了一个我需要的简单示例:http://jsbin.com/esalas/5

jQuery中没有内置函数可以实现这一点。解决方法是将元素封装在另一个元素中,然后获得其html()

$.each(arr, function(index, item) {
    var html = item.wrap("<div></div>").parent().html();
    console.log(html);
});

您也可以恢复到本机Javascript并使用outerHTML——尽管我还没有测试过:

$.each(arr, function(index, item) {
    console.log(item[0].outerHTML);
});

最新更新