简体   繁体   English

jQuery click事件,用于在不同点击中打开相同元素

[英]jquery click event for open same elements in different clicks

I have 2 blocks that have button #add-note with click event and this button should open block with .add-note class. 我有2个带有单击事件按钮#add-note的块,此按钮应使用.add-note类打开块。 If I click on any of these buttons it opens me 2 divs but I need that button opens 1 div that appera to these div. 如果我单击这些按钮中的任何一个,它将为我打开2格,但我需要该按钮为这些div打开一个1格。

$('#add-note').click(function() {
    $('.add-note').toggle('slow', function() {
        // Animation complete.
    });
});

<ul class="list-unstyled alerts-list contracts-alert-list">
                        <li>
                            <div class="alert-list-left">
                                <div class="alert-time  contracts-alert">
                                    <p>10:31 AM</p>
                                </div>
                                <p>
                                    <span class="alert-title">Alert 1</span>

                                </p>
                            </div>
                            <a href=""><i class="fa fa-times" aria-hidden="true"></i></a>
                        </li>
                        <li>
                            <div class="alert-list-left">
                                <div class="alert-time  contracts-alert">
                                    <p>10:31 AM</p>
                                </div>
                                <p>
                                    <span class="alert-title">Alert 1</span>

                                </p>
                            </div>
                            <a href=""><i class="fa fa-times" aria-hidden="true"></i></a>
                        </li>
                        <li>
                            <div class="alert-list-left">
                                <div class="alert-time  contracts-alert">
                                    <p>10:31 AM</p>
                                </div>
                                <p>
                                    <span class="alert-title">Alert 1</span>

                                </p>
                            </div>
                            <a href=""><i class="fa fa-times" aria-hidden="true"></i></a>
                        </li>
                        <li class="add-note add-note">
                            <form action="some-action" class="add-note-form">
                                <input type="text" class="form-control" placeholder="">
                                <button type="submit" class="btn form-submit-btn" id="submit-new-note-2">Add Note</button>
                            </form>
                        </li>
                    </ul>
                    <button type="button" class="btn add-alert-btn" id="add-note"><i class="fa fa-plus" aria-hidden="true"></i> Add Note Here</button>


<ul class="list-unstyled alerts-list contracts-alert-list">
                        <li>
                            <div class="alert-list-left">
                                <div class="alert-time  contracts-alert">
                                    <p>10:31 AM</p>
                                </div>
                                <p>
                                    <span class="alert-title">Alert 1</span>

                                </p>
                            </div>
                            <a href=""><i class="fa fa-times" aria-hidden="true"></i></a>
                        </li>
                        <li>
                            <div class="alert-list-left">
                                <div class="alert-time  contracts-alert">
                                    <p>10:31 AM</p>
                                </div>
                                <p>
                                    <span class="alert-title">Alert 1</span>

                                </p>
                            </div>
                            <a href=""><i class="fa fa-times" aria-hidden="true"></i></a>
                        </li>
                        <li>
                            <div class="alert-list-left">
                                <div class="alert-time  contracts-alert">
                                    <p>10:31 AM</p>
                                </div>
                                <p>
                                    <span class="alert-title">Alert 1</span>

                                </p>
                            </div>
                            <a href=""><i class="fa fa-times" aria-hidden="true"></i></a>
                        </li>
                        <li class="add-note add-note">
                            <form action="some-action" class="add-note-form">
                                <input type="text" class="form-control" placeholder="">
                                <button type="submit" class="btn form-submit-btn" id="submit-new-note-2">Add Note</button>
                            </form>
                        </li>
                    </ul>
                    <button type="button" class="btn add-alert-btn" id="add-note"><i class="fa fa-plus" aria-hidden="true"></i> Add Note Here</button>

There are a few issues with your code that have been mentioned in the comments, but I will sum then up here. 注释中已经提到了您的代码中的一些问题,但在这里我将总结一下。

  1. There are elements with the same ID. 有些元素具有相同的ID。 This would cause selection issues with the jQuery, as well as being something you should never do. 这将导致jQuery的选择问题,以及您永远都不应做的事情。 An ID name should never be used more than once on a page with no exceptions. 一个ID名称在一个页面上绝对不能被多次使用。

  2. The .add-note class was being added twice to the last li element. .add-note类被添加到最后一个li元素两次。 Although this woudn't cause an adverse effects, it isn't very good practice. 尽管这不会造成不利影响,但这不是很好的做法。

  3. The jQuery was targeting two elements to be toggled, not the element that relates to the clicked button. jQuery的目标是要切换的两个元素,而不是与单击按钮相关的元素。

Solution: 解:

When the click event is triggered you need to select the element that is relevant to the element that was clicked. 触发click事件时,您需要选择与单击的元素相关的元素。 The IDs have been switched to classes and the old .add-node li now has a class of note . 这些ID已切换为class,而旧的.add-node li现在有了一个note类。

So inside the click event you can use $(this) to specify the element that has been clicked, then use prev() to select the ul next to the clicked element and then find('.note') to select the element to be toggled. 因此,在click事件中,您可以使用$(this)指定已单击的元素,然后使用prev()选择被单击元素旁边的ul ,然后使用find('.note')选择要被单击的元素切换。

 $('.add-note').click(function() { $(this).prev().find('.note').toggle('slow', function() { // Animation complete. }); }); 
 <ul class="list-unstyled alerts-list contracts-alert-list"> <li> <div class="alert-list-left"> <div class="alert-time contracts-alert"> <p>10:31 AM</p> </div> <p> <span class="alert-title">Alert 1</span> </p> </div> <a href=""><i class="fa fa-times" aria-hidden="true"></i></a> </li> <li> <div class="alert-list-left"> <div class="alert-time contracts-alert"> <p>10:31 AM</p> </div> <p> <span class="alert-title">Alert 1</span> </p> </div> <a href=""><i class="fa fa-times" aria-hidden="true"></i></a> </li> <li> <div class="alert-list-left"> <div class="alert-time contracts-alert"> <p>10:31 AM</p> </div> <p> <span class="alert-title">Alert 1</span> </p> </div> <a href=""><i class="fa fa-times" aria-hidden="true"></i></a> </li> <li class="note"> <form action="some-action" class="add-note-form"> <input type="text" class="form-control" placeholder=""> <button type="submit" class="btn form-submit-btn submit-new-note-2">Add Note</button> </form> </li> </ul> <button type="button" class="btn add-alert-btn add-note"><i class="fa fa-plus" aria-hidden="true"></i> Add Note Here</button> <ul class="list-unstyled alerts-list contracts-alert-list"> <li> <div class="alert-list-left"> <div class="alert-time contracts-alert"> <p>10:31 AM</p> </div> <p> <span class="alert-title">Alert 1</span> </p> </div> <a href=""><i class="fa fa-times" aria-hidden="true"></i></a> </li> <li> <div class="alert-list-left"> <div class="alert-time contracts-alert"> <p>10:31 AM</p> </div> <p> <span class="alert-title">Alert 1</span> </p> </div> <a href=""><i class="fa fa-times" aria-hidden="true"></i></a> </li> <li> <div class="alert-list-left"> <div class="alert-time contracts-alert"> <p>10:31 AM</p> </div> <p> <span class="alert-title">Alert 1</span> </p> </div> <a href=""><i class="fa fa-times" aria-hidden="true"></i></a> </li> <li class="note"> <form action="some-action" class="add-note-form"> <input type="text" class="form-control" placeholder=""> <button type="submit" class="btn form-submit-btn submit-new-note-2">Add Note</button> </form> </li> </ul> <button type="button" class="btn add-alert-btn add-note"><i class="fa fa-plus" aria-hidden="true"></i> Add Note Here</button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

to only address the elements to the corresponding button, a wrapper is useful: 为了只将元素寻址到相应的按钮,包装器很有用:

<div class="listWrapper">
  <ul class="anUnorderedList" id="firstList">
    <li>some item</li>
    <li>some other item</li>
    <li class='inputItem'>
      <form>
        <input type='text' /> <span class="saveButton">save</span></form>
    </li>
  </ul>
  <div class="addButton">
    add another item to first list
  </div>
</div>

with this, you can bind a function to the button class and within the handle address the sibbling of the clicked button. 这样,您可以将函数绑定到按钮类,并在句柄地址内绑定单击的按钮。 i've done this, using parent->find: 我已经做到了,使用parent-> find:

$(".addButton").on("click", function() { 
  targetList = $(this).parent(".listWrapper").find("ul"); //find the corresponding ul

  targetList.find(".inputItem").toggle(); // toggle the li with class input item
});

see working fiddle, including a 'save' function: https://jsfiddle.net/x0ndL8h1/ 查看工作提琴,包括一个“保存”功能: https : //jsfiddle.net/x0ndL8h1/

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

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