jQuery 选择器返回的对象的完整 HTML
给定这个 html:
<li id="the_list_item"><img src="some_img"></li>
和这个选择器:
$("#the_list_item")
我想从 jQuery 选择器返回的对象中获取完整的 html。
使用:
$("#the_list_item").html()
...只是给了我内部html( 部分)
但是因为:
$("#the_list_item").attr("id")
给了我'the_list_item',这表明整个列表项确实是包含在返回的对象中..那么如何从该对象获取完整代码?
我想从我的对象中获取一个字符串:,但找不到方法去做它。
given this html:
<li id="the_list_item"><img src="some_img"></li>
and this selectior:
$("#the_list_item")
I want to get the full html from the object return by the jQuery selector.
Using:
$("#the_list_item").html()
...just gives me the inner html (the <img src="some_img">
part)
But since:
$("#the_list_item").attr("id")
gives me 'the_list_item', this indicated that the whole list item is indeed included in the object returned.. so how do I get the full code from that object?
I want to get a String: <li id="the_list_item"><img src="some_img"></li>
from my object, but can't find the way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
不幸的是,outerHTML 解决方案(上面接受的解决方案)在 FireFox 9 / Windows 7 / JQuery 1.7 上对我不起作用......
根据 Chetan Sastry(上面的答案),这是对我有用的东西:
Unfortunately, the outerHTML solution (accepted solution above) did not work for me on FireFox 9 / Windows 7 / JQuery 1.7 ...
Here is something that worked for me, according to Chetan Sastry (answer above):
您是否尝试过
$("#the_list_item").parent().html()
?Have you tried
$("#the_list_item").parent().html()
?在这里,您可以找到一个很好的解决方案,采用 jQuery externalHtml 插件代码的形式: http: //yelotofu.com/2008/08/jquery-outerhtml/
Here you can find a nice solution in the form of the code for a jQuery outerHtml plugin: http://yelotofu.com/2008/08/jquery-outerhtml/
创建一个新的 div DOM 对象并附加该 div 的克隆,然后获取包装的 div 的内容,最后删除创建的 DOM 对象。
Creating a new div DOM object and appending a clone of the div, then get the contents of wrapped div and finally removing the created DOM object.
有相应的插件。只需谷歌搜索“jQuery externalhtml”即可。其中大多数背后的想法是克隆元素,将其附加到空 div 并获取该 div 的 html。
There are plugins for that. Just google for "jQuery outerhtml". The idea behind most of them is to clone the element, append it to an empty div and get that div's html.
我不确定这是否有效,但可能值得一试:
I'm not sure if this works, but it might be worth a shot:
一种方法是创建您自己的包装器:
...做您的事情,然后展开:
One way is to create your own wrapper:
...do your thing, then unwrap:
留下以防万一现在有人仍在寻找这个。
完整的 jQuery 解决方案:
Leaving just in case someone nowadays is still looking for this.
Full jQuery solution:
这是我的解决方案,不需要 jQuery!
或者,也可以通过jQuery获取元素的引用,然后获取outerHTML。
Here my solution, No jQuery required!
Alternatively, it could be get the reference of element by means of jQuery, and then get the outerHTML.
jQuery 中没有等效的“外部 html”,但这可能会有所帮助:
http://jquery- howto.blogspot.com/2009/02/how-to-get-full-html-string-include.html
There's no "outer html" equivalent in jQuery, but this might help:
http://jquery-howto.blogspot.com/2009/02/how-to-get-full-html-string-including.html