javascript - IE,通过onclick方法发送 'this'参数

标签 javascript jquery html

我在 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()

Live Demo

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/

相关文章:

javascript - 检查是否使用 jquery 检查输入

Jquery Dropzone动态改变POST位置

html - html5 Canvas 中的滚动/滑动背景

javascript - 如何使谷歌表格函数引用调用它的单元格

javascript - 延迟 slideDown()/slideUp() - jquery 下拉列表

Javascript 遍历对象字面量

html - 只模糊一个巨大元素的背景

html - 如何在html中像表格一样改变div的方向

javascript - Webpack 2 未解析 webpack.config.js 中的入口点

javascript - JQuery 解析 JSON