我正在尝试在alert或console中显示jquery自定义属性。
我尝试使用console.log()来记录我的自定义数据属性,但它没有显示值。
我正在尝试显示此值数据 - 这是我的代码:
当我尝试在alert中显示值时,它显示未定义...如何修复它...
// 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 :(得分:1)
我改变了一些代码!
<div id="statusHtml">
</div>
<script>
// it will help to get statusHtml div tag using jquery.
var statusHtmlArr=$("#statusHtml");
$.each($('.loc-li'),funciton(){
alert("print the value in alert" + $(this).attr('data-test'));
});
/*OR*/
alert("print the value in alert" + $('.loc-li').attr('data-test'));
</script>
要在jquery中显示属性,你必须使用“attr()”函数
<强> Check Fiffle 强>