如何将jQuery对象转换为字符串?


当前回答

你能说得更具体一点吗?如果你想让HTML在一个标签里面,你可以这样做:

HTML代码片段:

<p><b>This is some text</b></p>

jQuery:

var txt = $('p').html(); // Value of text is <b>This is some text</b>

其他回答

在jQuery 1.6中,这似乎是一个更优雅的解决方案:

$('#element-of-interest').prop('outerHTML');

找出HTML节点(对象)可用的属性和方法的最好方法是这样做:

console.log($("#my-node"));

从jQuery 1.6+你可以使用outerHTML包括HTML标签在你的字符串输出:

var node = $("#my-node").outerHTML;

jQuery在这里,所以:

jQuery.fn.goodOLauterHTML= function() {
    return $('<a></a>').append( this.clone() ).html();
}

返回所有HTML内容:

$('div' /*elys with HTML text stuff that you want */ ).goodOLauterHTML(); // alerts tags and all

这对我来说似乎很有效:

$("#id")[0].outerHTML

不需要克隆和添加到DOM使用.html(),你可以做:

$('#item-of-interest').wrap('<div></div>').html()