简体   繁体   English

如何获取`data-listid`属性值

[英]How to get `data-listid` attribute value

In browsers console i am getting the below html element after using console.log in jquery script. 在浏览器控制台中,在jquery脚本中使用console.log之后,我得到了以下html元素。

<li class="fe_pui-autocomplete-box ui-draggable ui-sortable-helper" data-listid="latest_4604b40a-0492-49da-a86e-37f633501c2c" style="position: absolute; left: -561.812px; top: 98px; width: 456px; height: 14px; opacity: 1; z-index: 0;">.....</li>

I am using code like: 我正在使用类似的代码:

$(document).on('mouseup', 'li', function() 
{ 
console.log($(this)[0]); 
.....

How to get data-listid attribute value? 如何获取data-listid属性值?

我得到了解决方案:

console.log($(this)[0].attributes[1].value);

You can use .attr to get any attribute value: use: 您可以使用.attr来获取任何属性值:

console.log($(this).attr("data-listid"));

OR 要么

console.log($(this).data('listid'));

 console.log($('li').attr('data-listid')) //normally use .attr() console.log($('li').data('listid')) //if you set the attr using .data() you get it using .data(). 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="fe_pui-autocomplete-box ui-draggable ui-sortable-helper" data-listid="latest_4604b40a-0492-49da-a86e-37f633501c2c" style="position: absolute; left: -561.812px; top: 98px; width: 456px; height: 14px; opacity: 1; z-index: 0;">.....</li> </ul> 

  1. Normally you can use .attr() 通常您可以使用.attr()
  2. If you set the attr using .data() use need to use .data() to get it 如果使用.data()设置attr,则需要使用.data()来获取它

ex. 例如

you set the attr like 您将attr设置为

$('li').data('listid','adasdasdasdas') // use .data()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM