简体   繁体   English

将单击事件处理程序应用于多个元素

[英]Apply one click event handler to multiple elements

I'm trying to get my javascript to pop up a message under for each comment when its clicked. 我正试图让我的javascript在点击时为每条评论弹出一条消息。

I have 3 comment boxes, each has 1 "popup" button under it, and when the "popup" is clicked, it would alert a message ("Ouyeah!") 我有3个评论框,每个评论框下面有1个“弹出”按钮,当点击“弹出”时,它会提醒一条消息(“Ouyeah!”)

Only clicking the first "popup" link on the top alerts the message "Ouyeah!", and the message is alerted 3 times. 只需点击顶部的第一个“弹出窗口”链接即可发出消息“Ouyeah!”,并且该消息会被警告3次。 Clicking on other 2 "popup" links has no action. 点击其他2个“弹出窗口”链接无法执行操作。

Code: 码:

<div class="media">
<div class="media-body">
<p align="right">
<span><a href='javascript:;' id="reply_open">popup</a></span>
<div class="reply_form_div" STYLE="display:none;"></div>
</p>
</div>

<div class="media">
<div class="media-body">
<p align="right">
<span><a href='javascript:;' id="reply_open">popup</a></span>
<div class="reply_form_div" STYLE="display:none;"></div>
</p>
</div>

<div class="media">
<div class="media-body">
<p align="right">
<span><a href='javascript:;' id="reply_open">popup</a></span>
<div class="reply_form_div" STYLE="display:none;"></div>
</p>
</div>

JS: JS:

<script type="text/javascript">
$("#reply_open").parent().parent().parent().parent().children(".media-body").children("p").children("span").children("#reply_open").click(function(){
alert ("Ouyeah!");
</script>

Only clicking the first "popup" link on the top alerts the message "Ouyeah!", and the message is alerted 3 times. 只需点击顶部的第一个“弹出窗口”链接即可发出消息“Ouyeah!”,并且该消息会被警告3次。 Clicking on other 2 "popup" links has no action. 点击其他2个“弹出窗口”链接无法执行操作。

How to fix this? 如何解决这个问题? Many thanks, 非常感谢,

You cannot have duplicate IDs. 您不能拥有重复的ID。

Using id="reply_open" thrice (more than once!) is wrong and makes your HTML invalid. 使用id="reply_open"三次(不止一次!)是错误的,并使您的HTML无效。

JSFIDDLE DEMO -> http://jsfiddle.net/LC9gg/4/ JSFIDDLE DEMO - > http://jsfiddle.net/LC9gg/4/

This may be what you want. 这可能就是你想要的。 Make the IDs as classes & write the below code. 将ID作为类并编写以下代码。

$(".reply_open").click(function () {
    alert("Ouyeah!");
});

First of all, your HTML is incorrect in a few ways: 首先,您的HTML在以下几个方面是不正确的:

  • Your id's must be unique 你的id's必须是独一无二的
  • You didn't close your .media div tags. 您没有关闭.media div标签。
  • You're using JavaScript for a empty href 你正在使用JavaScript作为一个空的href

Try this instead: 试试这个:

<div class="media">
    <div class="media-body">
        <p align="right">
            <span><a href='./'>popup</a></span>
            <div class="reply_form_div" STYLE="display:none;"></div>
        </p>
    </div>
</div>

<div class="media">
    <div class="media-body">
        <p align="right">
            <span><a href='./'>popup</a></span>
            <div class="reply_form_div" STYLE="display:none;"></div>
        </p>
    </div>
</div>

<div class="media">
    <div class="media-body">
        <p align="right">
            <span><a href='./'>popup</a></span>
            <div class="reply_form_div" STYLE="display:none;"></div>
        </p>
    </div>
</div>

I removed the id 's altogether, they're not necessary. 我完全删除了id ,他们没有必要。 The .media divs are properly closed, and the JavaScript was removed from the hrefs. .media div正确关闭,JavaScript从href中删除。

Then, instead of searching through the DOM like you're doing with chained .parent() calls, try this js: 然后,不要像使用链式.parent()调用那样搜索DOM,而是试试这个js:

<script>
    $('.media .media-body a').click(function(){
        alert("Ouyeah!");
    });
</script>

This adds a event handler to each a , that should alert the desired text. 这会为每个a添加一个事件处理程序,它应该提醒所需的文本。

Working example 工作实例

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

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