59

可以说我有这个:

<div data-uid="aaa" data-name="bbb", data-value="ccc" onclick="fun(this.data.uid, this.data-name, this.data-value)">

还有这个:

function fun(one, two, three) {
    //some code
}

好吧,这不起作用,但我完全不知道为什么。有人可以发布一个工作示例吗?

4

6 回答 6

108

获取data-*属性的最简单方法是使用element.getAttribute()

onclick="fun(this.getAttribute('data-uid'), this.getAttribute('data-name'), this.getAttribute('data-value'));"

演示:http: //jsfiddle.net/pm6cH/


虽然我建议只传递this给,并函数中fun()获取 3 个属性:fun

onclick="fun(this);"

进而:

function fun(obj) {
    var one = obj.getAttribute('data-uid'),
        two = obj.getAttribute('data-name'),
        three = obj.getAttribute('data-value');
}

演示:http: //jsfiddle.net/pm6cH/1/


通过属性访问它们的新方法是 with dataset,但并非所有浏览器都支持。你会像下面这样得到它们:

this.dataset.uid
// and
this.dataset.name
// and
this.dataset.value

演示:http: //jsfiddle.net/pm6cH/2/


另请注意,在您的 HTML 中,此处不应有逗号:

data-name="bbb",

参考:

于 2013-05-15T13:26:56.097 回答
3

If you are using jQuery you can easily fetch the data attributes by

$(this).data("id") or $(event.target).data("id")
于 2018-11-15T10:33:21.813 回答
2

简短的回答是语法是this.dataset.whatever.

您的代码应如下所示:

<div data-uid="aaa" data-name="bbb" data-value="ccc"
    onclick="fun(this.dataset.uid, this.dataset.name, this.dataset.value)">

另一个重要的注意事项:无论您使用何种大小写,Javascript 总是会去除连字符并使数据属性为驼峰式。data-camelCase将成为this.dataset.camelcase并将data-Camel-case成为this.dataset.camelCase

jQuery(在 v1.5 和更高版本之后)总是使用小写字母,无论您的大小写如何。

因此,当使用此方法引用您的数据属性时,请记住 camelCase:

<div data-this-is-wild="yes, it's true"
    onclick="fun(this.dataset.thisIsWild)">

Also, you don't need to use commas to separate attributes.

于 2018-04-27T19:05:33.443 回答
1

HTML:

<div data-uid="aaa" data-name="bbb", data-value="ccc" onclick="fun(this)">

JavaScript:

function fun(obj) {
    var uid= $(obj).attr('data-uid');
    var name= $(obj).attr('data-name');
    var value= $(obj).attr('data-value');
}

但我正在使用 jQuery。

于 2013-05-15T13:31:07.087 回答
0

you might use default parameters in your function and then just pass the entire dataset itself, since the dataset is already a DOMStringMap Object

<div data-uid="aaa" data-name="bbb" data-value="ccc"
onclick="fun(this.dataset)">

<script>
const fun = ({uid:'ddd', name:'eee', value:'fff', other:'default'} = {}) { 
    // 
}
</script>

that way, you can deal with any data-values that got set in the html tag, or use defaults if they weren't set - that kind of thing

maybe not in this situation, but in others, it might be advantageous to put all your preferences in a single data-attribute

<div data-all='{"uid":"aaa","name":"bbb","value":"ccc"}'
onclick="fun(JSON.parse(this.dataset.all))">

there are probably more terse ways of doing that, if you already know certain things about the order of the data

<div data-all="aaa,bbb,ccc" onclick="fun(this.dataset.all.split(','))">
于 2019-08-21T17:53:16.203 回答
0

JS:

function fun(obj) {
    var uid= $(obj).data('uid');
    var name= $(obj).data('name');
    var value= $(obj).data('value');
}
于 2020-08-28T20:06:16.517 回答