简体   繁体   English

如何将click事件绑定到另一个click元素中的元素

[英]how to bind a click event to an element in another click element

The second click event that is binded to $("play") does not execute. 绑定到$(“ play”)的第二个click事件不会执行。 How can i bind another click event under a click event? 如何在点击事件下绑定另一个点击事件? I cant get the data newNumber if i write the second click event outside the first click event. 如果我在第一次单击事件之外编写第二次单击事件,则无法获取数据newNumber。

This piece of code is in the slide() function. 这段代码在slide()函数中。

var that = this;
$("#stop").click(function(){
    clearInterval(set);
    var newNumber = [num, numb, list]; //the value needed by click event that is binded to "play"

    $("play").click(newNumber, function() {
        num = newNumber[0];
        numb = newNumber[1];
        list = newNumber[2];
        that.slide(image, num, list, numb);
    });
});

You're probably confusing two important terms: binding and triggering . 您可能会混淆两个重要术语: bindingtriggering You probably want to trigger the second event within the handler of the first, but you want to bind both to begin with anyway. 您可能想在第一个事件的处理程序中触发第二个事件,但是无论如何您都想绑定两个事件。 Otherwise, you run into issues of binding the second event multiple times but never really triggering it. 否则,您将遇到多次绑定第二个事件但从未真正触发它的问题。

You normally want to bind an event once at the start, but you can trigger it as many times as you need. 通常, 一开始要绑定一个事件 ,但是您可以根据需要触发多次

var that = this;
var newNumber;
$("#stop").click(function(){
    clearInterval(set);
    newNumber = [num, numb, list]; //the value needed by click event that is binded to "play"
    $("#play").click(); // trigger the second <=======
});
$("#play").click(function() {
    num = newNumber[0];
    numb = newNumber[1];
    list = newNumber[2];
    that.slide(image, num, list, numb);
});

But the question I would ask is, What Does 'this' refer to? 但是我要问的问题是, “这个”指的是什么?

Use this - the right way is to trigger : 使用此方法-正确的方法是触发:

var that = this;
var newNumber = [];
$("#stop").click(function(){
    clearInterval(set);
    newNumber = [num, numb, list]; //the value needed by click event that is binded to "play"

   $("play").click() //or $("play").trigger('click')
});

$("play").click(function() {
        num = newNumber[0];
        numb = newNumber[1];
        list = newNumber[2];
        that.slide(image, num, list, numb);
});

  • Use .trigger() to bind a click event explicitly. 使用.trigger()显式绑定click事件。
  • Define the array newNumber in global scope , to use it in other events. 全局范围内定义数组newNumber,以在其他事件中使用它。

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

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