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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22625997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:29:13  来源:igfitidea点击:

How to add data to the anchor tag in HTML and access it using jQuery?

javascriptjqueryhtmlanchorhref

提问by PHPLover

Following is my HTML code of an anchor tag:

以下是我的锚标记的 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>

Now I want to add a question id to the above anchor tag and access it back in jQuery when user clicks on this hyperlink. For it I tried below code but it didn't work out for me.

现在我想向上面的锚标记添加一个问题 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>

The jQuery code for it is as follows:

它的jQuery代码如下:

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

It's giving me the message [object Object]in alert box. Can anyone please help me in setting the value to a anchor tag and accessing it in jQuery?

它在警报框中给我消息[object Object]。任何人都可以帮助我将值设置为锚标记并在 jQuery 中访问它吗?

回答by rajesh kakawat

try something like this

尝试这样的事情

html

html

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

javascript

javascript

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

you can add attribute data-sample_nameon your html element. In jquery use

您可以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

回答by Gaurang Tandon

I assume you are trying to do something like this:

我假设你正在尝试做这样的事情:

$(document).ready(function()  {
  // you can change the selector, `"key"` and its value below
  $("a.fixed").data("key", 21627); // on document ready, store the necessary data
  //                        ^-- Insert a dynamic value here if required

  $(".fixed").click(function(e) {    
    alert($(this).data("key"));  // 21627
  });

});

.data()stores a key-value pair. So here, I made a key called 'key'and stored with it a value of 21627and on click, alerted the value corresponding to the key 'key'.

.data()存储一个键值对。因此,在这里,我创建了一个键,'key'并与它一起存储了一个值,21627并在单击时提醒与该键对应的值'key'

You got a [object Object]because of the same reason that .data()stores data in an object and that by passing it zero arguments, you were essentially storing the object associated with .fixedinto t2.

您得到 a[object Object]的原因与.data()将数据存储在对象中以及通过向其传递零参数相同的原因,您实际上是在存储与.fixedinto关联的对象t2

回答by 151291

One more simple way is:

一种更简单的方法是:

  1. Use id attribute in anchor tag to write your data.
  1. 在锚标记中使用 id 属性来写入您的数据。

<a id="your-data" onclick="callfunction(this.id)">Fixed</a>

<a id="your-data" onclick="callfunction(this.id)">Fixed</a>

  1. Create a function in js file like callfucntion(id).
  1. 在 js 文件中创建一个函数,如callfucntion(id).

function callfucntion(id) { var data = id; // if more than one data, you can se split() }

function callfucntion(id) { var data = id; // if more than one data, you can se split() }