我正在尝试使用 jQuery 获取所选对象的 HTML。我知道.html()
功能;问题是我需要包含所选对象的 HTML(在本例中为表格行,其中.html()
仅返回行内的单元格)。
我四处搜索,发现了一些非常“hackish”类型的方法来克隆对象,将其添加到新创建的 div 等,但这看起来真的很脏。有没有更好的方法,或者新版本的 jQuery (1.4.2) 是否提供任何类型的outerHtml
功能?
我相信目前(5/1/2012),所有主流浏览器都支持outerHTML功能。在我看来,这个片段就足够了。我个人会选择记住这一点:
// Gives you the DOM element without the outside wrapper you want
$('.classSelector').html()
// Gives you the outside wrapper as well only for the first element
$('.classSelector')[0].outerHTML
// Gives you the outer HTML for all the selected elements
var html = '';
$('.classSelector').each(function () {
html += this.outerHTML;
});
//Or if you need a one liner for the previous code
$('.classSelector').get().map(function(v){return v.outerHTML}).join('');
EDIT: 基本支持统计 https://developer.mozilla.org/en-US/docs/DOM/element.outerHTML for element.outerHTML
- 火狐(壁虎):11 ....发布时间 2012-03-13 https://wiki.mozilla.org/Releases
- 铬:0.2 ......发布时间 2008-09-02 http://en.wikipedia.org/wiki/Google_Chrome#Release_history
- 互联网浏览器 4.0...1997 年发布 http://en.wikipedia.org/wiki/History_of_Internet_Explorer
- 歌剧 7 ......................发布时间 2003-01-28 http://www.opera.com/docs/history/#o70
- Safari 1.3 ......................发布时间 2006-01-12 http://en.wikipedia.org/wiki/Apple_Safari#Safari_1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)