[Front-end] jquery gets the attribute value of data-*

 Get the value of the following data-id through jquery

<div id="getId" data-id="122" >获取id</div>

Method 1: dataset() method

//data-前缀属性可以在JS中通过dataset取值,更加方便
console.log(getId.dataset.id);//112

//赋值
getId.dataset.id = "113";//113

//新增data属性
getId.dataset.id2 = "100";//100

//删除,设置成null,或者delete
getId.dataset.id2 = null;//null
delete getId.dataset.id2;//undefind

Method 2: jquery data() method

// 取值
var id = $("#getId").data("id"); //122

//赋值
$("#getId").data("id","100");//100

Get the currently clicked attribute value through the click event

// 获取当前点击的属性值
$("#getId).click(function(){
    var id= $(this).data('id')
})

Method three: jquery attr() method

// 取值
var id = $("#getId").attr("data-id"); //122

//赋值
$("#getId").attr("data-id","100");//100

Guess you like

Origin blog.csdn.net/qq_25285531/article/details/132901163