如何将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

其他回答

你能说得更具体一点吗?如果你想让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在这里,所以:

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

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

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

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

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

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

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

可以使用jQuery.makeArray(obj)实用函数:

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