获取所选元素的外部HTML [英] Get selected element's outer HTML

查看:86
本文介绍了获取所选元素的外部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).

我到处搜索,发现了一些克隆对象,将其添加到新创建的div等中的非常"hackish"类型的方法,等等,但这似乎很脏.有没有更好的方法,还是新版本的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆