任何人都知道如何从这一点获取outerHTML?....
$("#sectionA tr[data-testlog='"+variableName+"']").first().outerHTML;
返回未定义的....
答案 0 :(得分:1)
outerHTML
是一个DOM属性,因此您首先必须访问包含在jQuery对象中的DOM元素
您可以使用each
$(/*any jquery stuff*/).each(function(idx, domEl) {
console.log(domEl.outerHTML);
});
如果你知道你只有一个匹配,或者只想获得第一个元素,你应该这样做:
$(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
可能(或可能不会)在所有浏览器/平台上得到同等支持。