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

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

问题描述

我正在使用jQuery quicksand插件。我需要获取所单击项的data-id并将其传递给webservice。
如何获取data-id属性?我正在使用 .on()方法重新绑定已排序项目的click事件。

I'm using the jQuery quicksand plugin. I need to get the data-id of the clicked item and pass it to a webservice. 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> ;链接< / 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-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天全站免登陆