2013-06-26 116 views
0

我正在检索JavaScript函数中html标记的值,但是现在如果我想检索html的数据属性值并将其传递给JavaScript变量。将数据属性值传递给JavaScript变量

我该怎么办?

// create a UL element 
var ul = $('<ul></ul>'); 

// add some LI elements 
ul.append(
    $('<li class="bear" data-type="panda">panda bear</li>'), 
    $('<li class="bear" data-type="koala">koala bear</li>'), 
    $('<li class="wallaby" data-type="kangaroo">kangaroo</li>') 
); 

// this won't work because our UL is not in the dom yet 
console.log($('.bear').length); //=> 0 

// let's just pick the bears in our UL using our UL as the context 
var bears = $('.bear', ul); 
console.log(bears); //=> [li.bear, li.bear] 

// lets loop through and see data attributes 
bears.each(function() { 
    console.log($(this).attr('data-type')); 
}); 
//=> "panda" 
//=> "koala" 

回答

0

我认为这个问题是您不使用此

statusHtmlArr.push('<li class="loc-li" data-test="' + rowData.APPT_LOC_QUAL[k].LOCATION_CD + '" value="' + rowData.APPT_LOC_QUAL[k].LOCATION_CD + '"><a class="sub-detail" href="Javascript:void(0);">' + rowData.APPT_LOC_QUAL[k].LOCATION + '</a></li>'); 
+0

所以如何解决它.... – user2475135

0

字符串连接为了检索元素的属性,它是通过使用jQuery容易。

$('#selectorId').attr('attribute name') 

$('.className').attr('attribute name') 
+0

,但如何将其传递到JS变量LOCID – user2475135

+0

'LOCID = $( '#selectorId')。attr('attribute name');'因为它返回一个字符串结果。 –

+0

在这里,我使用的是一个班级,而不是一个班级,所以我该怎么做 – user2475135

0

你可以这样做:

var locID = $("li.loc-li")[0].data("test"); 
相关问题