获取所选元素的外部 HTML [英] Get selected element's outer HTML
问题描述
我正在尝试使用 jQuery 获取选定对象的 HTML.我知道 .html()
函数;问题是我需要包含所选对象的 HTML(在本例中为表格行,其中 .html()
仅返回行内的单元格).
I'm trying to get the HTML of a selected object with jQuery. I am aware of the .html()
function; the issue is that I need the HTML including the selected object (a table row in this case, where .html()
only returns the cells inside the row).
我四处搜索并发现了一些非常hackish"类型的克隆对象、将其添加到新创建的 div 等的方法,但这看起来很脏.有没有更好的方法,或者新版本的 jQuery (1.4.2) 是否提供任何类型的 outerHtml
功能?
I've searched around and found a few very ‘hackish’ type methods of cloning an object, adding it to a newly created div, etc, etc, but this seems really dirty. Is there any better way, or does the new version of jQuery (1.4.2) offer any kind of outerHtml
functionality?
推荐答案
2014 问题和此回复来自 2010 年.当时,没有更好的解决方案可以广泛使用.现在,许多其他回复都更好:例如 Eric Hu 或 Re Capcha.
该站点似乎为您提供了解决方案:jQuery:outerHTML |油豆腐
This site seems to have a solution for you : jQuery: outerHTML | Yelotofu
jQuery.fn.outerHTML = function(s) {
return s
? this.before(s).remove()
: jQuery("<p>").append(this.eq(0).clone()).html();
};
这篇关于获取所选元素的外部 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!