$(this) 在 onclick 属性中不起作用
$(this) does not work inside onclick attribute
<head>
<script>
function call() {
var id = $(this).data("id");
alert(id);
}
</script>
</head>
<body>
<div data-id="5" onclick="call();"></div>
</body>
这是我在<script>
标签之间的javascript函数。当它调用id
显示为undefined
.为什么$(this)
不起作用?
您必须将元素作为函数的参数传递call()
如下所示。
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function call(elm) {
var id = $(elm).data("id");
alert(id);
}
</script>
</head>
<body>
<div data-id="5" onclick="call(this);">Click Here</div>
</body>
相关文章:
- 在jQuery中链接时CSS属性不起作用
- 函数中的 angularJS 访问对象属性不起作用
- 使用 css() 更改自定义属性不起作用
- 调用类属性不起作用
- 为什么onclick属性不起作用
- JQuery Smart Spinner:回调属性不起作用
- “document.write”的字体属性不起作用
- 行数文本输入属性不起作用
- CSS 显示属性不起作用
- 必需属性不起作用
- HTML 中文本框上的必需属性不起作用
- 使用变量的 Href 属性不起作用
- 使用 javascript 修改 svg 属性不起作用
- JS:为什么这个对象属性不起作用,只有当我把它作为变量放在其中一个方法中时,它才有效
- j查询验证引擎属性不起作用
- AngularJS “{{}}” insite title 属性不起作用
- JQuery css 属性不起作用
- 设置属性不起作用
- 使用量角器获取按钮上的禁用属性不起作用
- KnockoutJS和计算属性不起作用