javascript – 如何将数据添加到HTML中的锚标签并使用jQuery访问它?

前端之家收集整理的这篇文章主要介绍了javascript – 如何将数据添加到HTML中的锚标签并使用jQuery访问它?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
以下是我的锚标记HTML代码

<a delhref="http://localhost/eprime/entprm/web/control/modules/questions/manage_question_issue.PHP?op=fixed&question_id=21627&que_issue_status=0" title="Fixed" href="#fixedPopContent" class="fixed">Fixed</a>

现在我想在上面的锚标记添加一个问题id,并在用户点击这个超链接时在jQuery中访问它.对于它我尝试下面的代码,但它没有为我工作.

<a delhref="http://localhost/eprime/entprm/web/control/modules/questions/manage_question_issue.PHP?op=fixed&question_id=21627&que_issue_status=0" title="Fixed" href="#fixedPopContent" class="fixed" data="21627">Fixed</a>

它的jQuery代码如下:

$(document).ready(function()  {
  $(".fixed").click(function(e) {
    var t2 = $(this).data();
    alert(t2);
  });
});

它在警告框中给我消息[对象对象].任何人都可以帮助我将值设置为锚标记并在jQuery中访问它?

解决方法

尝试这样的事情

HTML

<a  href="#fixedPopContent" class="fixed" data-q_id="21627"></a>

JavaScript的

$(document).ready(function()  {
  $(".fixed").click(function(e) {
    var t2 = $(this).data('q_id');
    alert(t2);
  });
});

您可以在html元素上添加属性data-sample_name.
在jquery中使用

$('your_element_id').data('sample_name');// to get value
$('your_element_id').data('sample_name','new value');// to set  value

猜你在找的jQuery相关文章