jQuery 访问 html5 数据属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16936162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery accessing html5 data attribute
提问by Adam
On click of one of the options I am trying to get the data value from the 'ul li a' and place it into button below, I've set up a fiddle example here: http://jsfiddle.net/q5j8z/4/
单击其中一个选项时,我试图从“ul li a”中获取数据值并将其放入下面的按钮中,我在这里设置了一个小提琴示例:http: //jsfiddle.net/q5j8z/4 /
But cant seem to get it working
但似乎无法让它工作
$('ul li a').click(function(e) {
e.preventDefault();
var value = $(this).data();
$('.button').data('value');
});
Does anyone have any ideas please?
请问有人有什么想法吗?
采纳答案by pala?н
You can do this:
你可以这样做:
$('ul li a').click(function (e) {
e.preventDefault();
var value = $(this).data('value');
$('.button').data('value', value);
console.log($('.button').data('value'));
});
Here, $(this).data('value')
is used to get the data
attribute value of the link.
这里,$(this).data('value')
用于获取data
链接的属性值。
and $('.button').data('value', value)
is used to set the data
attribute value of the button.
和$('.button').data('value', value)
用于设置data
按钮的属性值。
Using, console.log($('.button').data('value'));
you can check the console the data
value being set.
使用,console.log($('.button').data('value'));
您可以检查控制台data
设置的值。
For more info:- .data() API Documentation
更多信息:- .data() API 文档
回答by A. Wolff
Use it like this:
像这样使用它:
var value = $(this).data('value');
And then:
进而:
$('.button').data('value', value);
回答by thomas
You are not assigning the value data to the button actually. Try this:
您实际上并未将值数据分配给按钮。尝试这个:
$('ul li a').click(function(e) {
e.preventDefault();
var value = $(this).data();
// assign the value form the clicked anchor to button value data
$('.button').data('value', value);
console.log($('.button').data('value'));
});
回答by Alessandro Minoccheri
Try this:
尝试这个:
$('ul li a').click(function(e) {
e.preventDefault();
var value = $(this).data('value');
$('.button').data('value', value);
});
You can see the value of the button with console.log into your console with:
您可以使用 console.log 在控制台中看到按钮的值:
console.log('data: '+$(".button").data("value"));
回答by Ted
First off you are are you trying to insert the value of variable value
or 'value' literal?
首先,您是要插入变量value
的值还是“值”字面量?
data()
is meant to be used similarily to attr('attr-name', attr_value)
like below:
data()
旨在attr('attr-name', attr_value)
类似于以下使用:
$('ul li a').click(function(e) {
e.preventDefault();
var value = $(this).data('value');
$('.button').data('value', value);
});
The above works in your fiddle.
以上适用于您的小提琴。
回答by Emeka Mbah
Please note that you don't need to specify data- prefix because Jquery does that automatically
请注意,您不需要指定 data- 前缀,因为 Jquery 会自动执行此操作
So ensure you are using the right attribute for value below
因此,请确保您使用正确的属性作为下面的值
$('ul li a').click(function(e) {
e.preventDefault();
var value = $(this).data('value');
$('.button').data('value', value);
});