我是Java语言的新手。我想编写一个JavaScript代码,当我单击按钮时,警报窗口会弹出并写入data-message属性。这是我的代码:
<button type="button" data-message="a1" onclick="pop()">click</button>
<script>
function pop() {
alert(this.getAttribute("data-message"));
}
</script>
但是我得到了错误
TypeError: this.getAttribute is not a function
alert(this.getAttribute("data-message"));
我有两个问题:
怎么了?
我该如何调试?我如何找出this
指的是什么?我正在使用萤火虫。
非常感谢。
您需要this
像这样发送按钮
<button type="button" data-message="a1" onclick="pop(this)">click</button>
和Js,捕获谁在调用它。
function pop(e) {
alert(e.getAttribute("data-message"));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句