简体   繁体   English

为什么不能<a>从html</a>获取<a>标记</a>的值

[英]why cant get the value of <a> tag from html

I don't know whats the problem of my JavaScript or my code. 我不知道我的JavaScript或代码有什么问题。
I can't get the data-value of an <a> tag from HTML. 我无法从HTML获取<a>标记的data-value
I don't know what's wrong because I am just a beginner in JavaScript. 我不知道怎么了,因为我只是JavaScript的初学者。

I have a loop where I have many categories in my website and I put a value of id from my <a> tag. 我有一个循环,其中我的网站中有很多类别,并且我在<a>标记中添加了id值。
Here is the code: 这是代码:

<div class="list-group">
  <?php foreach($data as $single_data) { ?>
  <a href="#" id="target" data-value="<?php echo $single_data->Category_id; ?>" class="list-group-item"><?= $single_data->Category_name ?></a>
  <?php } ?>
</div>

When i go to show page source it looks good and look like this: 当我去显示页面源代码时,它看起来不错,看起来像这样:

<a href="#" id="target" data-value="1" class="list-group-item">Gadgets</a>
<a href="#" id="target" data-value="2" class="list-group-item">Books</a>

Now here is my JavaScript: 现在这是我的JavaScript:

$("#target").click(function() {
    var value = $(this).data("value");
    alert(value);
});

Id of the tag should be unique 标签的ID应该是唯一的
You can do something like 你可以做类似的事情

<a href="#" id="target1" data-value="1" class="list-group-item">Gadgets</a>
<a href="#" id="target2" data-value="2" class="list-group-item">Books</a>

Or if you want to capture click event for both anchor tags you can use class selector instead 或者,如果您想捕获两个定位标记的click事件,则可以改用类选择器

 $(".list-group-item").click(function() { var value = $(this).data("value"); alert(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" id="target" data-value="1" class="list-group-item">Gadgets</a> <a href="#" id="target" data-value="2" class="list-group-item">Books</a> 

id need to be unique. id必须是唯一的。 You can use class as a selector in your problem 您可以在问题中使用class作为选择器

 $(".list-group-item").click(function() { var value = $(this).data("value"); alert(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" data-value="1" class="list-group-item">Gadgets</a> <a href="#" data-value="2" class="list-group-item">Books</a> 

you need to change two things : 您需要更改两件事:

  • id of elements have to be unique 元素的ID必须是唯一的
  • bound each element with a click() function. 用click()函数绑定每个元素。

And html side : 和html端:

<a href="#" id="targetGadget" data-value="1" class="list-group-item">Gadgets</a>
<a href="#" id="targetBook" data-value="2" class="list-group-item">Books</a>

JS side : JS端:

$("#targetGadget").click(function() {
    var value = $(this).data("value");
    alert(value);
});
$("#targetBook").click(function() {
    var value = $(this).data("value");
    alert(value);
});

Now, from JS side, to not repeat yourself, a better way would be to add <a> elements in a div in the html side and in the js dynamically applying the click() function for each of <a> child of the div. 现在,从JS方面开始,为了不重复自己,一种更好的方法是在html端的div中添加<a>元素,并在js中为div的每个<a>子元素中的每个动态应用click()函数。

Here is an example : 这是一个例子:

html : html:

div id="targets">
  <a href="#" id="targetGadget" data-value="1" class="list-group-item">Gadgets</a>
  <a href="#" id="targetBook" data-value="2" class="list-group-item">Books</a>
</div>

js : js:

var children = $("#targets").get(0).children;
for (var i = 0; i < children.length; i++) {
    var child = children[i];
    $(child).click(function() {
        var value = $(this).data("value");
        alert(value);
    });
}

IDs are always unique. ID始终是唯一的。 You CANNOT have same ID to different DOM elements. 不同的DOM元素不能具有相同的ID。 In that case, assign them a class. 在这种情况下,请为他们分配一个班级。

Changed the ID to class, and accordingly made change in JS code: 将ID更改为class,并相应地更改了JS代码:

 $(".target").click(function() { var value = $(this).data("value"); alert(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" class="target list-group-item" data-value="1">Gadgets</a> <a href="#" class="target list-group-item" data-value="2">Books</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM