简体   繁体   English

使用同一按钮多次触发JQUERY功能

[英]Fire JQUERY function more than once using the same button

I have 5 divisions in the html code and after the first division a button "show more" has been placed. 我在html代码中有5个分区,在第一个分区之后,放置了一个“显示更多”按钮。 I want to fire a jquery command which shows the second division on clicking the button "show more" once and the third division on again clicking the button and so on. 我想触发一个jquery命令,该命令在单击按钮“显示更多”时显示第二部分,在再次单击按钮时显示第三部分,依此类推。

The problem is that i am only able to show the second division and not able to fire the jquery script the second time to show the third division. 问题是我只能显示第二部分,而不能第二次触发jquery脚本以显示第三部分。

below is my Jquery and html. 下面是我的Jquery和html。

 $(document).ready(function(){ for(i=2;i<=5;i++) { $("#"+i).hide(); } i=2; /* degrade the value of i */ $("button").on("click",function(){ //alert(i); $("#button").remove(); $("#"+i).show(); var button="<button id='button' class='btn btn-primary'>show more</button>"; $("#"+i).after(button); i=i+1; }); }); 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col-md-4 col-sm-6 col-xs-12" id="1" style="border:1px solid red; height:200px; width:300px;"></div> <button id='button' class='btn btn-primary'>show more</button> <div class="col-md-4 col-sm-6 col-xs-12" id="2" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="3" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="4" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="5" style="border:1px solid red; height:200px; width:300px;"></div> </div><!-- .row --> </div><!-- .container --> 

You can do this way. 您可以这样做。 The trick is: 诀窍是:

$(":hidden").first().show()

Snippet 片段

 $(function () { $("p:not(:first)").hide(); $("a").click(function (e) { e.preventDefault(); $("p:hidden").first().show(); }); }); 
 * {margin: 0; padding: 0; list-style: none; line-height: 1;} p {display: inline-block; padding: 5px; border: 1px solid #ccc;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>Paragraph 1</p> <p>Paragraph 2</p> <p>Paragraph 3</p> <p>Paragraph 4</p> <p>Paragraph 5</p> <a href="#">Show</a> 

Also, you can add one more check to find if there are any more hidden <p> and then you can remove the link too: 另外,您可以再添加一项检查以查找是否还有其他隐藏的<p> ,然后您也可以删除链接:

 $(function () { $("p:not(:first)").hide(); $("a").click(function (e) { e.preventDefault(); $("p:hidden").first().show(); if (!$("p:hidden").length) $(this).hide(); }); }); 
 * {margin: 0; padding: 0; list-style: none; line-height: 1;} p {display: inline-block; padding: 5px; border: 1px solid #ccc;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>Paragraph 1</p> <p>Paragraph 2</p> <p>Paragraph 3</p> <p>Paragraph 4</p> <p>Paragraph 5</p> <a href="#">Show</a> 

Well I guess a very short and clean solution is this: https://jsfiddle.net/hallleron/h6huonub/1/ 好吧,我猜这是一个非常简短的解决方案: https : //jsfiddle.net/hallleron/h6huonub/1/

Use the detach() function to handle the button. 使用detach()函数处理按钮。 This way you DO NOT lose the event delegation of the button and it is way less resource intensive. 这样,您就不会丢失按钮的事件委托,并且不会占用大量资源。

The key part is this line: 关键部分是这一行:

var el = $('#button').detach();

 $(document).ready(function(){ for(i=2;i<=5;i++) { $("#"+i).hide(); } i=2; $("button").on("click",function(){ var el = $('#button').detach(); $("#"+i).show(); $("#"+i).after(el); i=i+1; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col-md-4 col-sm-6 col-xs-12" id="1" style="border:1px solid red; height:200px; width:300px;"></div> <button id='button' class='btn btn-primary'>show more</button> <div class="col-md-4 col-sm-6 col-xs-12" id="2" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="3" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="4" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="5" style="border:1px solid red; height:200px; width:300px;"></div> </div><!-- .row --> </div><!-- .container --> 

EDIT: Added working code snippet. 编辑:添加了工作代码段。

It's because your second button is not in the DOM when you initialize your function. 这是因为初始化函数时,第二个按钮不在DOM中。

Instead use this: 而是使用此:

    $('body').on('click', 'button', function(){
       // Your code
    });

http://api.jquery.com/on/ http://api.jquery.com/on/

 $(document).ready(function(){ for(i=2;i<=5;i++) { $("#"+i).hide(); } i=2; /* degrade the value of i */ $("body").on("click", "button" ,function(){ //alert(i); $("#button").remove(); $("#"+i).show(); var button="<button id='button' class='btn btn-primary'>show more</button>"; $("#"+i).after(button); i=i+1; }); }); 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col-md-4 col-sm-6 col-xs-12" id="1" style="border:1px solid red; height:200px; width:300px;"></div> <button id='button' class='btn btn-primary'>show more</button> <div class="col-md-4 col-sm-6 col-xs-12" id="2" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="3" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="4" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="5" style="border:1px solid red; height:200px; width:300px;"></div> </div><!-- .row --> </div><!-- .container --> 

Why are you destroying your button, then trying to re-create it immediately after? 为什么要销毁按钮,然后尝试立即重新创建按钮? Just use the same button. 只需使用相同的按钮。 Otherwise you'll need delegation for dynamically added elements. 否则,您将需要委派动态添加的元素。

$("#button").click( () => $(`#${++i}`).show())

Or in ES5: 或在ES5中:

$("#button").click( function(){ $("#" + (++i) }).show())

You say that you're destroying the button and re-creating it so it's always after the last shown element, well, place the button directly after all hidden elements and leave it here... 您说要销毁按钮并重新创建它,所以它总是在最后显示的元素之后,好吧,将按钮直接放在所有隐藏的元素之后,并留在这里...

When you remove the button and insert a new one, it does not have your click handler attached to it. 当您删除按钮并插入一个新按钮时,它没有附加单击处理程序。 What you can do instead is move the existing button, which will retain the click handler. 相反,您可以做的是移动现有按钮,该按钮将保留点击处理程序。

 $(document).ready(function() { var i, $button = $('#button'); for (i = 2; i <= 5; i++) { $("#" + i).hide(); } i = 2; /* degrade the value of i */ $("button").on("click", function() { var $i = $("#" + i); $button.insertAfter($i); $i.show(); i = i + 1; }); }); 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col-md-4 col-sm-6 col-xs-12" id="1" style="border:1px solid red; height:200px; width:300px;"></div> <button id='button' class='btn btn-primary'>show more</button> <div class="col-md-4 col-sm-6 col-xs-12" id="2" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="3" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="4" style="border:1px solid red; height:200px; width:300px;"></div> <div class="col-md-4 col-sm-6 col-xs-12" id="5" style="border:1px solid red; height:200px; width:300px;"></div> </div> <!-- .row --> </div> <!-- .container --> 

Indeed, the documentation of insertAfter mentions that 确实, insertAfter的文档中提到

If an element selected this way is inserted into a single location elsewhere in the DOM, it will be moved after the target (not cloned) and a new set consisting of the inserted element is returned: 如果以这种方式选择的元素插入到DOM中其他位置的单个位置,则将在目标(未克隆)之后移动该元素,并返回由插入的元素组成的新集合:

(Emphasis mine.) (强调我的。)

I think you are looking for 我想你在找

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    </head>
    <div class="container">
        <div class="row">
            <div class="col-md-4 col-sm-6 col-xs-12" id="ID_1" style="border:1px solid red; height:200px; width:300px;"></div>
            <button id='button' class='btn btn-primary'>show more</button>


      </div><!-- .row -->
    </div><!-- .container -->

<!-- end snippet -->

<script>

    var id=1;
        $(document).ready(function(){

                $("body").on("click", "button" ,function(){
                id++;

                var div='<div class="col-md-4 col-sm-6 col-xs-12" id="ID_'+id+'" style="border:1px solid red; height:200px; width:300px;"></div>';
                    var button="<button id='button' class='btn btn-primary'>show more</button>";
                $("#button").remove();
                $(".row").append(div);
                    $(".row").append(button);
                });
            });

</script>

Try to do this: 尝试这样做:

$(document).ready(function() {
    for (var i = 2; i <= 5; i++) {
        $("#" + i).hide();
    }
    var i = 2; /* degrade the value of i */

    $("button").on("click", function() {
        $("#" + i).show();
        i = i + 1;
    });
});

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

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