简体   繁体   English

jQuery 点击功能仅适用于第一个元素

[英]jQuery click function only works on first element

I am having some trouble with jQuery.我在使用 jQuery 时遇到了一些问题。

I am making a simple CMS and in the interface I have a list of pages, in each list item is an edit link.我正在制作一个简单的 CMS,在界面中我有一个页面列表,每个列表项中都有一个编辑链接。 I made jQuery listen for clicks with that edit id.我让 jQuery 监听具有该编辑 ID 的点击。 It will then look at the parent LI to see what id it has so the user can save the changes to the right pageId in the database.然后它会查看父 LI 以查看它有什么 id,以便用户可以将更改保存到数据库中正确的 pageId。

My List我的列表

<ul id="sortable" class="ui-sortable">
    <li class="sortable" id="listItem_1">
        <a href="#" id="edit">edit</a>
        <span id="title">List item 1</span>
    </li>

    <li class="sortable" id="listItem_2">
        <a href="#" id="edit">edit</a>
        <span id="title">List item 2</span>
    </li>

    etc..
</ul>

And the javascript和 javascript

<script type="text/javascript">
$(document).ready(function() {
    $('a#edit').click(function(){
        alert($(this).parent("li").attr("id"));
    })
});

But only the first edit link works.但只有第一个编辑链接有效。 All the others just get ignored.所有其他人都被忽略了。 You can see the problem working here, http://info.radio-onair.ath.cx/active/scms/admin/pages/test.html您可以在这里看到问题,http: //info.radio-onair.ath.cx/active/scms/admin/pages/test.html

Thanks in advance.提前致谢。

In HTML, id refers to a unique identifier .在 HTML 中, id指的是唯一标识符 In other words, it is against standards to have 2 elements with the same id .换句话说,具有相同id 2 个元素是违反标准的。 jQuery here behaves correctly. jQuery 在这里的行为是正确的。

Use a class instead of an id to identify your tags as such:使用class而不是id来标识您的标签,如下所示:

HTML: HTML:

<ul id="sortable" class="ui-sortable">
    <li class="sortable" id="listItem_1">
        <a class="edit" href="#">edit</a>
        <span id="title">List item 1</span>
    </li>

    <li class="sortable" id="listItem_2">
        <a class="edit" href="#">edit</a>
        <span id="title">List item 2</span>
    </li>

    etc..
</ul>

JavaScript: JavaScript:

$(document).ready(function() {
        $('a.edit').click(function(){
                alert($(this).parent("li").attr("id"));
        })
});

Alternatively, since the parent tag already seems to have a unique class, you could simply use it to target wanted tags.或者,由于父标签似乎已经有一个唯一的类,您可以简单地使用它来定位想要的标签。 This would reduce what I call "class noise" (the defining of useless class to target element which could be targeted by their parent's unique attributes).这将减少我所说的“类噪音”(将无用类定义为目标元素,这些元素可以由其父级的独特属性作为目标)。

HTML: HTML:

<ul id="sortable" class="ui-sortable">
    <li class="sortable" id="listItem_1">
        <a href="#">edit</a>
        <span id="title">List item 1</span>
    </li>

    <li class="sortable" id="listItem_2">
        <a href="#">edit</a>
        <span id="title">List item 2</span>
    </li>

    etc..
</ul>

JavaScript: JavaScript:

$(document).ready(function() {
        $("li.sortable a:contains('edit')").click(function(){
                alert($(this).parent("li").attr("id"));
        })
});

I think this is because use use the same id for each element.我认为这是因为对每个元素使用相同的id Try using a class instead.尝试改用类。

<a href="#" class="edit">edit</a>

And then接着

$('a.edit').click(...)

You can't have two elements with the same ID.不能有两个具有相同 ID 的元素。 That's invalid HTML.那是无效的 HTML。 Perhaps you want a class instead?也许你想要一堂课?

Try:尝试:

<ul id="sortable" class="ui-sortable">
    <li class="sortable" id="listItem_1">
        <a href="#" class="edit">edit</a>
        <span id="title">List item 1</span>
    </li>

    <li class="sortable" id="listItem_2">
        <a href="#" class="edit">edit</a>
        <span id="title">List item 2</span>
    </li>

    etc..
</ul>


<script type="text/javascript">
$(document).ready(function() {
    $('a.edit').click(function(){
        alert($(this).parent("li").attr("id"));
    })
});
</script>       

ID 必须是唯一的,而类名可以相同。

我尝试了您的链接,看起来所有警报都已连接并正常工作,只是没有按顺序排列(2 是 5 等)

If you don't want to change your HTML (but you should) you can try this:如果你不想改变你的 HTML(但你应该),你可以试试这个:

$(document).ready(function() {
        $('a:contains("edit")').click(function(){
                alert($(this).parent("li").attr("id"));
        })
});

I have no idea what everyone else said, but I just changed an我不知道其他人说了什么,但我只是改变了一个

<a id="anyidname".. 

from fancy box... to this script at the end of my html/php page... and the tag to从花哨的盒子......到我的html/php页面末尾的这个脚本......以及标签

<a rel="anyclassname"..

<script type="text/javascript">
 $(document).ready(function(){
$('a[rel="anyclassname"]').fancybox({
    'width'         : '75%',
    'height'        : '75%',
    'autoScale'         : false,
    'transitionIn'      : 'none',
    'transitionOut'     : 'none',
    'type'          : 'iframe'
});
  });
</script>"

jquery class click function may work only on the first class element as jquery 类点击功能可能只对第一个类元素起作用

$('.class_name').click(function(){

but it should work for all class elements as但它应该适用于所有类元素

$(document).on('click', '.class_name', function(){

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

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