[英]how to bind a click event to an element in another click element
綁定到$(“ play”)的第二個click事件不會執行。 如何在點擊事件下綁定另一個點擊事件? 如果我在第一次單擊事件之外編寫第二次單擊事件,則無法獲取數據newNumber。
這段代碼在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);
});
});
您可能會混淆兩個重要術語: binding
和triggering
。 您可能想在第一個事件的處理程序中觸發第二個事件,但是無論如何您都想綁定兩個事件。 否則,您將遇到多次綁定第二個事件但從未真正觸發它的問題。
通常, 您一開始要綁定一個事件 ,但是您可以根據需要觸發多次 。
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);
});
但是我要問的問題是, “這個”指的是什么?
使用此方法-正確的方法是觸發:
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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.