Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

javascript - 如何获取data-id属性?(How to get the data-id attribute?)

I'm using the jQuery quicksand plugin.

(我正在使用jQuery quicksand插件。)

I need to get the data-id of the clicked item and pass it to a webservice.

(我需要获取所单击项的data-id并将其传递给webservice。)

How do I get the data-id attribute?

(如何获取data-id属性?)

I'm using the .on() method to re-bind the click event for sorted items.

(我正在使用.on()方法重新绑定已排序项的click事件。)

 $("#list li").on('click', function() { // ret = DetailsView.GetProject($(this).attr("#data-id"), OnComplete, OnTimeOut, OnError); alert($(this).attr("#data-id")); }); 
 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script> <ul id="list" class="grid"> <li data-id="id-40" class="win"> <a id="ctl00_cphBody_ListView1_ctrl0_SelectButton" class="project" href="#"> <img src="themes/clean/images/win.jpg" class="project-image" alt="get data-id" /> </a> </li> </ul> 

  ask by Bruce Adams translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get the contents of the attribute data-id (like in <a data-id="123">link</a> ) you have to use

(要获取属性data-id的内容(如在<a data-id="123">link</a> ),您必须使用)

$(this).attr("data-id") // will return the string "123"

or .data() (if you use newer jQuery >= 1.4.3)

(或.data() (如果你使用更新的jQuery> = 1.4.3))

$(this).data("id") // will return the number 123

and the part after data- must be lowercase, eg data-idNum will not work, but data-idnum will.

(并且data-之后的部分必须是小写的,例如data-idNum将不起作用,但data-idnum将起作用。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...