jquery和js元素找到每个元素的data-index索引号
    		       		warning:
    		            这篇文章距离上次修改已过451天,其中的内容可能已经有所变动。
    		        
        		                
                要使用jQuery或JavaScript获取每个元素的data-index属性,可以使用.data()方法。以下是使用jQuery和JavaScript的示例代码:
使用jQuery获取data-index属性:
$('[data-index]').each(function() {
    var index = $(this).data('index');
    console.log(index);
});使用JavaScript获取data-index属性:
document.querySelectorAll('[data-index]').forEach(function(element) {
    var index = element.dataset.index;
    console.log(index);
});这两段代码都会选择页面上所有具有data-index属性的元素,并打印出它们的索引号。
评论已关闭