2013-04-15 32 views

回答

1

outerHTML是DOM属性,因此你首先必须访问DOM元素包裹你的jQuery对象中

可以使用each

$(/*any jquery stuff*/).each(function(idx, domEl) { 
    console.log(domEl.outerHTML); 
}); 

如果你硝酸钾这样做w ^,你有一个确切的匹配,或想只得到第一个元素,你应该做的:

$(SELECTOR)[0].outerHTML // or access any other **DOM** property 

你的情况:

$("#sectionA tr[data-testlog='"+variableName+"']")[0].outerHTML 
// first() would be redundant and unnecessary 

基本上你能想到的jQuery对象作为一个包装围绕一系列DOM元素,增加了一大堆令开发人员开心的功能,并且功能跨浏览器兼容。

根据MDN outerHTML的支持:

Firefox (Gecko): 11 since 2012-03-13 
Chrome: 0.2   since 2008-09-02 
Internet Explorer 4.0 since 1997 
Opera 7    since 2003-01-28 
Safari 1.3    since 2006-01-12 

或者为akluth建议你可以

// select your element 
$(SELECTOR) 
// clone it (outside the dom of the page) 
.clone() 
// wrap it in another element 
.wrap('<div>') 
// get the parent - basically the wrapper you just added 
.parent() 
// and get its inner html - all the html your SELECTOR initially selected 
.html(); 

// all in one line 
$(SELECTOR).clone().wrap('<div>').parent().html(); 

这将是jQuery的都做了更昂贵的操作等问题,但也将是一个交叉浏览器解决方案。 outerHTML可能(或不可能)在所有浏览器/平台上同等支持。

+0

好吧Matyas,这是一个很大的帮助 –

相关问题