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

PHP*_*ver 4 html javascript anchor jquery href

以下是我的锚标记的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>
Run Code Online (Sandbox Code Playgroud)

现在我想在上面的锚标记中添加一个问题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>
Run Code Online (Sandbox Code Playgroud)

它的jQuery代码如下:

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

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

raj*_*wat 11

尝试这样的事情

HTML

 <a  href="#fixedPopContent" class="fixed" data-q_id="21627"></a>
Run Code Online (Sandbox Code Playgroud)

JavaScript的

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

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

$('your_element_id').data('sample_name');// to get value
$('your_element_id').data('sample_name','new value');// to set  value
Run Code Online (Sandbox Code Playgroud)