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


当前回答

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

其他回答

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

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

你能说得更具体一点吗?如果你想让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.makeArray(obj)实用函数:

var obj = $('<p />',{'class':'className'}).html('peekaboo');
var objArr = $.makeArray(obj);
var plainText = objArr[0];
new String(myobj)

如果您想将整个对象序列化为字符串,请使用JSON。

这对我来说似乎很有效:

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