如何获取 data-id 属性? [英] How can I get the data-id attribute?

查看:147
本文介绍了如何获取 data-id 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery Quicksand 插件.我需要获取点击项目的数据 ID 并将其传递给网络服务.

I'm using the jQuery Quicksand plugin. I need to get the data-id of the clicked item and pass it to a webservice.

如何获取 data-id 属性?我正在使用 .on() 方法重新绑定已排序项目的点击事件.

How do I get the data-id attribute? I'm using the .on() method to re-bind the click event for sorted items.

$("#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>

推荐答案

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

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

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

.data()(如果您使用较新的 jQuery >= 1.4.3)

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

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

data-之后的部分必须小写,例如data-idNum 不起作用,但 data-idnum 会起作用.

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

这篇关于如何获取 data-id 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆