如何从数据属性中获取数据密钥名称?
我想从我的html元素中获取关键名称?
示例代码:
220
使用jquery数据方法我能够键值,但我想提取键名?
var keyValue=$("td").data("code"); //123 var keyName=?????
数据代码将是关键。
如果要获取未知键/值对的键,可以使用for (var key in data) {}
循环:
var all_values = [], data = $('td').data(); for (var key in data) { all_values.push([key, data[key]]); } //you can now access the key/value pairs as an array of an array //if $(td).data() returns: `{code : 123}` then this code would return: [ [code, 123] ] //you could get the first key with: all_values[0][0] and its corresponding value: all_values[0][1]
您可以通过以下方式访问所有数据键和值:
$.each($("td").data(), function(key, value) { console.log(key + ": " + value); });
这是一个例子。