Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检索jQuery中*all*子元素的数据属性_Javascript_Jquery_Html - Fatal编程技术网

Javascript 检索jQuery中*all*子元素的数据属性

Javascript 检索jQuery中*all*子元素的数据属性,javascript,jquery,html,Javascript,Jquery,Html,我想以数组的形式获取子元素的所有数据属性 我试过了 "data_array" : $('#rule-list').children().data('fruits') <div class="fruit-list" id="fruit-list"> <span class="chips" title="Click to remove" data-fruit="apple,mango; pineapple, watermelon">every month

我想以数组的形式获取子元素的所有数据属性

我试过了

"data_array" : $('#rule-list').children().data('fruits')


 <div class="fruit-list" id="fruit-list">
        <span class="chips" title="Click to remove" data-fruit="apple,mango; pineapple, watermelon">every month for 5 times</span>
        <span class="chips" title="Click to remove" data-fruit="banana,kiwi">every May and November for 5 times</span>
    </div>
var-elements=document.querySelectorAll(“[data-fruit]”);
var结果=[];
对于(var i=0;i

每月5次
每年5月和11月连续5次

由于要返回每个数组元素的属性值,而不是单个元素的属性值,因此不能使用(因为它只返回jQuery集合中第一个元素的
数据-*
属性)

我建议在调用中使用
getAttribute
,而不是:

var fruitList=document.getElementById('fruit-list'))
var DATARRAY=[].map.call(水果列表.children,函数(e){
返回e.getAttribute('data-fruit')
})
console.log(数据数组)

每月5次
每年5月和11月连续5次
您可以这样做

var fruitArray=[] // Array to contain array of fruits
// will get all children
var getChild = $("#fruit-list").children();
// loop over them
getChild.each(function(i,v){
// push in fruits array, an array of data-fruit
fruitArray.push([($(v).data('fruit'))])
})
console.log(fruitArray)

您可以使用选择器
“#水果列表[数据水果]”
$.map()
.match()
RegExp
/\w+//code>作为参数来匹配一个或多个单词字符,返回返回值为
.match()
的数组以获得多维数组的结果

var arr=$.map($(“#结果列表[数据结果]”),函数(el){
返回数组($(el).data(“fruit”).match(/\w+/g))
});
控制台日志(arr)

每月5次
每年5月和11月连续5次
var fruitArray=[] // Array to contain array of fruits
// will get all children
var getChild = $("#fruit-list").children();
// loop over them
getChild.each(function(i,v){
// push in fruits array, an array of data-fruit
fruitArray.push([($(v).data('fruit'))])
})
console.log(fruitArray)