Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery隐藏按钮_Javascript_Jquery - Fatal编程技术网

Javascript 使用jquery隐藏按钮

Javascript 使用jquery隐藏按钮,javascript,jquery,Javascript,Jquery,我想使用jquery而不是内联函数隐藏单击按钮 我有HTML: <button id="mybutton" value="click me">click me</button> 该按钮不会在单击时隐藏。我做错了什么?如果你能看到公认的答案,你就会知道问题出在哪里 问题是当您定义一个函数时,比如 var funcName = function(){...} 所以在这种情况下,你必须在定义之后调用这个函数。否则它将不可用 像下面这样定义你的函数 function hide

我想使用jquery而不是内联函数隐藏单击按钮

我有HTML:

<button id="mybutton" value="click me">click me</button>

该按钮不会在单击时隐藏。我做错了什么?

如果你能看到公认的答案,你就会知道问题出在哪里

问题是当您定义一个函数时,比如

var funcName = function(){...}
所以在这种情况下,你必须在定义之后调用这个函数。否则它将不可用

像下面这样定义你的函数

function hideelement() {
    $(this).hide();
}

或者像这样改变顺序

hideelement = function() {
    $(this).hide();
}

$("#mybutton").on("click", hideelement); 
hideelement = function() {
    $(this).hide();
}

$("#mybutton").on("click", hideelement); 

如果你能看到公认的答案,你就会知道问题出在哪里

问题是当您定义一个函数时,比如

var funcName = function(){...}
所以在这种情况下,你必须在定义之后调用这个函数。否则它将不可用

像下面这样定义你的函数

function hideelement() {
    $(this).hide();
}

或者像这样改变顺序

hideelement = function() {
    $(this).hide();
}

$("#mybutton").on("click", hideelement); 
hideelement = function() {
    $(this).hide();
}

$("#mybutton").on("click", hideelement); 

尝试此
$(“#mybutton”)。单击(函数(){$(this.hide()})

试试这个
$(“#mybutton”)。点击(function(){$(this.hide()})

我一直在盯着屏幕看。在反转函数定义并设置单击行为后,它起作用了


我一直在盯着屏幕看。在反转函数定义并设置单击行为后,它起作用了


添加到已经工作的解决方案中,并提供更多细节


文件准备就绪
首先,确保将代码放置在
正文的底部或类似于以下内容的内部,以确保在执行代码时元素位于DOM中:

$(document).ready(function(){
   ... your code here...
});
$(“#mybutton”)
在执行代码时将不会在DOM中,并且不会绑定任何事件


可变起重
如果这不是问题所在,并且事件已绑定,您可能会发现代码的编写方式导致错误:
Uncaught ReferenceError:hideelement未定义

原因是

上述代码实际上由JavaScript解释器解释如下:

var hideelement; // declaration was hoisted to the top of the scope

$("#mybutton").on("click", hideelement); // at this point hideelement is still 'undefined'

hideelement = function(){ // assignment of the value stays within the lexical scope
    $(this).hide();
}
function hideelement(){
    $(this).hide();
}

$("#mybutton").on("click", hideelement); // hideelements is defined
JavaScript会将声明提升到当前范围的顶部,但会将赋值留在定义它的词法范围中

但是,如果您将声明更改为:

$("#mybutton").on("click", hideelement);

function hideelement(){
    $(this).hide();
}
JavaScript现在对上述代码的解释如下:

var hideelement; // declaration was hoisted to the top of the scope

$("#mybutton").on("click", hideelement); // at this point hideelement is still 'undefined'

hideelement = function(){ // assignment of the value stays within the lexical scope
    $(this).hide();
}
function hideelement(){
    $(this).hide();
}

$("#mybutton").on("click", hideelement); // hideelements is defined
由于
hideelements
不再是一个赋值,而只是一个声明,因此整个函数将被提升,并在事件绑定使用时定义



当然,在使用作业之前,先用词汇定义作业的建议解决方案也会起作用。i、 e:

hideelement = function() {
    $(this).hide();
}

$("#mybutton").on("click", hideelement); 
为了简单起见,我故意没有进入全球范围 范围和使用
var
的区别


添加到已经工作的解决方案中,并提供更多细节


文件准备就绪
首先,确保将代码放置在
正文的底部或类似于以下内容的内部,以确保在执行代码时元素位于DOM中:

$(document).ready(function(){
   ... your code here...
});
$(“#mybutton”)
在执行代码时将不会在DOM中,并且不会绑定任何事件


可变起重
如果这不是问题所在,并且事件已绑定,您可能会发现代码的编写方式导致错误:
Uncaught ReferenceError:hideelement未定义

原因是

上述代码实际上由JavaScript解释器解释如下:

var hideelement; // declaration was hoisted to the top of the scope

$("#mybutton").on("click", hideelement); // at this point hideelement is still 'undefined'

hideelement = function(){ // assignment of the value stays within the lexical scope
    $(this).hide();
}
function hideelement(){
    $(this).hide();
}

$("#mybutton").on("click", hideelement); // hideelements is defined
JavaScript会将声明提升到当前范围的顶部,但会将赋值留在定义它的词法范围中

但是,如果您将声明更改为:

$("#mybutton").on("click", hideelement);

function hideelement(){
    $(this).hide();
}
JavaScript现在对上述代码的解释如下:

var hideelement; // declaration was hoisted to the top of the scope

$("#mybutton").on("click", hideelement); // at this point hideelement is still 'undefined'

hideelement = function(){ // assignment of the value stays within the lexical scope
    $(this).hide();
}
function hideelement(){
    $(this).hide();
}

$("#mybutton").on("click", hideelement); // hideelements is defined
由于
hideelements
不再是一个赋值,而只是一个声明,因此整个函数将被提升,并在事件绑定使用时定义



当然,在使用作业之前,先用词汇定义作业的建议解决方案也会起作用。i、 e:

hideelement = function() {
    $(this).hide();
}

$("#mybutton").on("click", hideelement); 
为了简单起见,我故意没有进入全球范围 范围和使用
var
的区别


“请勿重新发明控制盘”的可能副本:
$(“#mybutton”).on(“单击”,函数(){$(this.hide();})
$(“#mybutton”)。单击(函数(){$(this.hide();})如果您查看浏览器控制台,我很确定那里有错误。@MelanciaUK:匿名函数很难进行单元测试。命名函数不仅是很好的实践,而且可以帮助您编写可测试代码,并支持将DOM操作与业务逻辑分离。另外,
在本场景中使用命名函数可以完美地保存。可能重复的“请勿重新发明控制盘:
$(“#mybutton”)。在(“单击”,函数(){$(this.hide();})
$(“#mybutton”)。单击(函数(){$(this.hide();})如果您查看浏览器控制台,我很确定那里有错误。@MelanciaUK:匿名函数很难进行单元测试。命名函数不仅是很好的实践,而且可以帮助您编写可测试代码,并支持将DOM操作与业务逻辑分离。另外,
在本场景中使用命名函数可以完美地保存。示例:和@showdev thanx将有帮助。示例:和@showdev thanx将有帮助。虽然这起作用,但它不能解释OPs代码为什么不起作用。虽然它起作用,但它不能解释OPs代码为什么不起作用。