我在 html 中有 onclick 方法,它将“this”发送到 js 函数。在 chrome 上它可以工作,我可以找到 closest("tr") 元素。但是在 IE 浏览器中我得到一个错误:
Object doesn't support property or method 'closest'
我的 html:
<td class="border-0" style="vertical-align: middle; width: 20px;">
<a href="javascript:void(0)" onclick="SearchLossCodeDescDam(this);" tabindex="-1">
<div title="Search Loss Code" class="img-responsive claim-ccc-search_bttn"></div>
</a>
</td>
和js方法:
function SearchLossCodeDescDam(s) {
var row = s.closest("tr");
.....
}
我做了一个样本:https://jsfiddle.net/b0oa9yL6/
请问现在我该如何解决这个问题?
最佳答案
您正在将 DOM 对象传递给 javacript 函数。将 DOM 对象转换为 jQuery 对象以对其调用 closest
。要从 jQuery 对象获取 ID,您可以使用 attr()
var row = $(s).closest("tr");
alert(row.attr("id"));
为什么它在 chrome 中有效
这在 Chrome 中有效,可能 chrome 浏览器使用 Element.closest() .这就是为什么您能够访问 id 属性,因为 Element.closest
返回 DOM 而不是 jQuery 对象。
The Element.closest() method returns the closest ancestor of the current element (or the current element itself) which matches the selectors given in parameter. If there isn't such an ancestor, it returns null.
关于javascript - IE,通过onclick方法发送 'this'参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37067085/