Sorry, if the title is too obscure ;D.
对不起,如果标题过于模糊; D。
Actually the problem is, lets say i am this code.
实际上问题是,让我说我是这个代码。
<span id="spanIDxx" style="blah-blah">
<tag1>code here </tag2> sample text
<tag2>code here </tag2> html aass hhddll
sample text
</span>
Now if i will use the code.
现在,如果我将使用代码。
jQuery("#spanIDxx").html();
then it will return just the innerHTML excluding <span id="spanIDxx" style="blah-blah">
but i want something which can return the innerHTML including the specified element.
那么它将只返回innerHTML,不包括但是我想要一些可以返回包含指定元素的innerHTML的东西。
4 个解决方案
#1
11
This will create a new div
and append a clone of your element to that div
. The new div
never gets inserted into the DOM, so it doesn't affect your page.
这将创建一个新的div并将元素的克隆附加到该div。新的div永远不会插入到DOM中,因此它不会影响您的页面。
var theResult = $('<div />').append($("#spanIDxx").clone()).html();
alert( theResult );
If you need to use this frequently, and don't want to bother with adding yet another plugin, just make it into a function:
如果你需要经常使用它,并且不想再添加另一个插件,只需将它变成一个函数:
function htmlInclusive(elem) { return $('<div />').append($(elem).clone()).html(); }
alert( htmlInclusive("#spanIDxx") );
Or extend jQuery yourself:
或者自己扩展jQuery:
$.fn.htmlInclusive = function() { return $('<div />').append($(this).clone()).html(); }
alert( $("#spanIDxx").htmlInclusive() );
#2
1
You can copy the node to a new empty node, ask for new .parent() and then its .html()
您可以将节点复制到新的空节点,请求新的.parent()然后请求.html()
#3
1
Clone might be useful to you. I don't believe you actually need to do anything with the clone/s.
克隆可能对您有用。我不相信你真的需要对clone / s做任何事情。
#4
0
Haven't used this myself but looks like it may be of use:
我自己没有使用它,但看起来它可能有用:
http://yelotofu.com/2008/08/jquery-outerhtml/
http://yelotofu.com/2008/08/jquery-outerhtml/
demo here: http://yelotofu.com/labs/jquery/snippets/outerhtml/demo.html
这里演示:http://yelotofu.com/labs/jquery/snippets/outerhtml/demo.html
#1
11
This will create a new div
and append a clone of your element to that div
. The new div
never gets inserted into the DOM, so it doesn't affect your page.
这将创建一个新的div并将元素的克隆附加到该div。新的div永远不会插入到DOM中,因此它不会影响您的页面。
var theResult = $('<div />').append($("#spanIDxx").clone()).html();
alert( theResult );
If you need to use this frequently, and don't want to bother with adding yet another plugin, just make it into a function:
如果你需要经常使用它,并且不想再添加另一个插件,只需将它变成一个函数:
function htmlInclusive(elem) { return $('<div />').append($(elem).clone()).html(); }
alert( htmlInclusive("#spanIDxx") );
Or extend jQuery yourself:
或者自己扩展jQuery:
$.fn.htmlInclusive = function() { return $('<div />').append($(this).clone()).html(); }
alert( $("#spanIDxx").htmlInclusive() );
#2
1
You can copy the node to a new empty node, ask for new .parent() and then its .html()
您可以将节点复制到新的空节点,请求新的.parent()然后请求.html()
#3
1
Clone might be useful to you. I don't believe you actually need to do anything with the clone/s.
克隆可能对您有用。我不相信你真的需要对clone / s做任何事情。
#4
0
Haven't used this myself but looks like it may be of use:
我自己没有使用它,但看起来它可能有用:
http://yelotofu.com/2008/08/jquery-outerhtml/
http://yelotofu.com/2008/08/jquery-outerhtml/
demo here: http://yelotofu.com/labs/jquery/snippets/outerhtml/demo.html
这里演示:http://yelotofu.com/labs/jquery/snippets/outerhtml/demo.html