jQuery 如何查找数据元素值
jQuery how to find an data element value
我有这个 html 代码:
<body>
....
<span data-code="code100">...</span>
....
<div data-code="code101">...</div>
<input type="test" ... data-code="code102">...</input>
...
<b data-code="code103">...</b>
etc...
</body>
现在我想找到所有带有[data-code]的元素并获取data-code值(code100, code101, code 103, code104)
这不起作用:
var arr = $("[data-code]");
$.each(arr, function(key) {
alert($(key).data('data-code'));
});
我该怎么做?
对我有用。使用打开的开发人员控制台尝试此示例。
var arr = $("[data-code]");
$.each(arr, function(index, value) {
console.log($(this).attr('data-code'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span data-code="code100"></span>
<div data-code="code101"></div>
<input type="test" data-code="code102" />
<b data-code="code103"></b>
这项工作-
var arr = $("[data-code]");
$.each(arr, function(index, value) {
alert($(this).data('code'));
});
您可以这样引用数据值。您的数据密钥是 'code'。
阅读更多关于 Jquery.data() here
我有这个 html 代码:
<body>
....
<span data-code="code100">...</span>
....
<div data-code="code101">...</div>
<input type="test" ... data-code="code102">...</input>
...
<b data-code="code103">...</b>
etc...
</body>
现在我想找到所有带有[data-code]的元素并获取data-code值(code100, code101, code 103, code104)
这不起作用:
var arr = $("[data-code]");
$.each(arr, function(key) {
alert($(key).data('data-code'));
});
我该怎么做?
对我有用。使用打开的开发人员控制台尝试此示例。
var arr = $("[data-code]");
$.each(arr, function(index, value) {
console.log($(this).attr('data-code'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span data-code="code100"></span>
<div data-code="code101"></div>
<input type="test" data-code="code102" />
<b data-code="code103"></b>
这项工作-
var arr = $("[data-code]");
$.each(arr, function(index, value) {
alert($(this).data('code'));
});
您可以这样引用数据值。您的数据密钥是 'code'。 阅读更多关于 Jquery.data() here