-1

在数据属性被识别后,我一直在尝试访问数据和嵌套元素,但我没有运气,这里是代码:

 $('#item-prize-location').find('[data-region]').each(function(){


  //want to access data-region value
      //want to access nested divs with        

 });

当我做一个console.log(this) 是给我所有的嵌套元素,但我不知道如何访问它们或数据区域的值。

4

3 回答 3

0
 $('#item-prize-location').find('[data-region]').each(function(){

mydata=$(this).attr('data-region') //or mydata=$(this).data('region') 
//do stuff with mydata-- that is the data in the selected element
//for example:
console.log(mydata)
//You can access the element itself using `this` or $(this)


 });

$.each总是返回元素。您需要重新访问函数内部的数据。

于 2013-02-12T06:55:59.903 回答
0

试试这个:试试小提琴

$('#item-prize-location').find('[data-region]').each(function(){
   console.log($(this).data('region'));       
});
于 2013-02-12T06:56:09.157 回答
0

您可以使用dataset属性或 jQuerydata方法:

$('#item-prize-location').find('[data-region]').each(function(){
      var region = this.dataset.region;
      // var region = $(this).data('region');
});

您还可以使用map方法并将值存储在数组中。

var regions = $('#item-prize-location div[data-region]').map(function(){
      return this.dataset.region;
}).get();
于 2013-02-12T06:54:39.097 回答