Javascript 如何从另一个函数绑定Dom$(this)
请看一下这段代码,让我看看如何在dom事件内部的外部函数中绑定Javascript 如何从另一个函数绑定Dom$(this),javascript,jquery,Javascript,Jquery,请看一下这段代码,让我看看如何在dom事件内部的外部函数中绑定$(this) $(“.adder”)。在(“单击”,函数()上){ updateText(); }); 函数updateText(){ $(this.next(“.mapper”).html(“明白了!”); } 左边 中间的 赖特 左边 中间的 赖特 您应该能够将其作为参数传递: $(".adder").on("click", function(){ var node = $(this); updateText
$(this)
$(“.adder”)。在(“单击”,函数()上){
updateText();
});
函数updateText(){
$(this.next(“.mapper”).html(“明白了!”);
}
左边
中间的
赖特
左边
中间的
赖特
您应该能够将其作为参数传递:
$(".adder").on("click", function(){
var node = $(this);
updateText(node);
});
function updateText(node){
node.next(".mapper").html("Got You!");
}
您应该能够将其作为参数传递:
$(".adder").on("click", function(){
var node = $(this);
updateText(node);
});
function updateText(node){
node.next(".mapper").html("Got You!");
}
一种方法是:
function updateText(){
$(this).next(".mapper").html("Got You!");
}
$(".adder").on("click", updateText);
另一个是
function updateText(){
$(this).next(".mapper").html("Got You!");
}
$(".adder").on("click", function(){
updateText.bind(this)();
});
一种方法是:
function updateText(){
$(this).next(".mapper").html("Got You!");
}
$(".adder").on("click", updateText);
另一个是
function updateText(){
$(this).next(".mapper").html("Got You!");
}
$(".adder").on("click", function(){
updateText.bind(this)();
});