获取html元素的onclick事件的data- *属性 [英] Getting data-* attribute for onclick event for an html element
本文介绍了获取html元素的onclick事件的data- *属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<a id="option1" data-id="10" data-option="21" href="#" onclick="goDoSomething(?,?);">
Click to do something
</a>
我想获得 data-id
函数内的 data-option
值 goDoSomething(10,21)
我试过用这个
参考: this.data ['id']
但它没有用。
I want to get the data-id
and data-option
values inside the function goDoSomething(10, 21)
I have tried to use this
reference: this.data['id']
but it did not work.
我该怎么做?
推荐答案
你可以实现这个 $(标识符).data ('id')
使用 jquery ,
<script type="text/javascript">
function goDoSomething(identifier){
alert("data-id:"+$(identifier).data('id')+", data-option:"+$(identifier).data('option'));
}
</script>
<a id="option1"
data-id="10"
data-option="21"
href="#"
onclick="goDoSomething(this);">
Click to do something
</a>
javascript :您可以使用 getAttribute( attributename)
如果想使用javascript标签,
javascript : You can use getAttribute("attributename")
if want to use javascript tag,
<script type="text/javascript">
function goDoSomething(d){
alert(d.getAttribute("data-id"));
}
</script>
<a id="option1"
data-id="10"
data-option="21"
href="#"
onclick="goDoSomething(this);">
Click to do something
</a>
或者:
<script type="text/javascript">
function goDoSomething(data_id, data_option){
alert("data-id:"+data_id+", data-option:"+data_option);
}
</script>
<a id="option1"
data-id="10"
data-option="21"
href="#"
onclick="goDoSomething(this.getAttribute('data-id'), this.getAttribute('data-option'));">
Click to do something
</a>
这篇关于获取html元素的onclick事件的data- *属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文