Javascript 将按钮的实例传递给其onclick函数jquery
在我的程序中,我将按钮的onclick行为更改为:Javascript 将按钮的实例传递给其onclick函数jquery,javascript,jquery,html,Javascript,Jquery,Html,在我的程序中,我将按钮的onclick行为更改为: button.attr('onclick','function1()'); 我想给function1()传递一个按钮的实例,因为有很多按钮,它们可能会在单击它们时访问function1(),并且知道它们的父母是我的逻辑必须的 这可能吗 button.attr('onclick','function1(this);'); 您可以将“this”传递给函数,然后该参数将成为按钮 function function1(myBut
button.attr('onclick','function1()');
我想给function1()传递一个按钮的实例,因为有很多按钮,它们可能会在单击它们时访问function1(),并且知道它们的父母是我的逻辑必须的
这可能吗
button.attr('onclick','function1(this);');
您可以将“this”传递给函数,然后该参数将成为按钮
function function1(myButton){
//do stuff
}
或者,您可以使用jquery和匿名函数
$(button).click(function()
{
var myButton = this; // in this scope "this" is the button.
});
您可以将“this”传递给函数,然后该参数将成为按钮
function function1(myButton){
//do stuff
}
或者,您可以使用jquery和匿名函数
$(button).click(function()
{
var myButton = this; // in this scope "this" is the button.
});
您始终可以传递
此上下文并使用它
button.attr('onclick','function1(this)');
但是为什么要将内联事件附加到按钮上呢。直接附加事件是更好的做法
button.on('click', someFunction);
function someFunction() {
this
// this corresponds to the button that is currently clicked.
}
您始终可以传递此上下文并使用它
button.attr('onclick','function1(this)');
但是为什么要将内联事件附加到按钮上呢。直接附加事件是更好的做法
button.on('click', someFunction);
function someFunction() {
this
// this corresponds to the button that is currently clicked.
}
不要那样做。在()上使用
。看医生,我会的。我知道,我只是习惯性地写在这里,不要那样做。在()上使用
。看医生,我会的。我知道我只是出于习惯写在这里。我不能使用匿名函数,我认为这是因为onclick行为在嵌套函数中发生了变化。当我执行myButton.val()或myButton.parent()时,它没有运行?我想我不能使用匿名函数,因为这种onclick行为在嵌套函数中会发生变化。当我执行myButton.val()或myButton.parent()时,它是否未运行?