这个JQuery在哪里错了? [英] Where is this JQuery wrong?

查看:128
本文介绍了这个JQuery在哪里错了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将点击事件添加到id =violacao的所有元素:

I want to add the click event to all elements where the `id="violacao":

$(document).ready(function () {
    jQuery('#violacao').click(function() {
        alert('teste');
    });
});

但是只有第一个链接会响应点击。这是HTML生成的:

But just the first link responds to the click. This is the HTML generated:

<tr>
  <td><a href="#" id="violacao">40954589</a></td>
  <td>Perda de Comunicação</td>
</tr>

<tr>
  <td><a href="#" id="violacao">88692020503</a></td>
  <td>Perda de Comunicação</td>
</tr>

当我尝试这种方式:

jQuery("a").click(function() {
    alert('teste');
});

它工作正常,但所有链接都受影响。错误是什么?

It works fine, except that all links are affected. What is wrong?

推荐答案

HTML中的ID应该是唯一的(每个文档一个)。将ID更改为类(并使用。而不是#),它应该工作。

IDs in HTML are meant to be unique (one per document). Change the ID to a class (and use . instead of #) and it should work.

这篇关于这个JQuery在哪里错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆