javascript 检查按钮是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20065314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if a button exists
提问by John Java
I have a .js file that is invoked by a few .jsp pages. What I need to do in this particular .js file is to invoke a certain function only if the 'save' button is present in the .jsp page. How do I check if the the button exists? Currently in the .js file the button is refered to this way: $('button[id=save]')
我有一个由几个 .jsp 页面调用的 .js 文件。我需要在这个特定的 .js 文件中做的是仅当 .jsp 页面中存在“保存”按钮时调用某个函数。如何检查按钮是否存在?目前在 .js 文件中,按钮是这样引用的: $('button[id=save]')
How do I check if such a button exists? Hope someone can advise. Thanks.
如何检查这样的按钮是否存在?希望有人能指教。谢谢。
回答by rajesh kakawat
try something like this
尝试这样的事情
$(document).ready(function(){
//javascript
if(document.getElementById('save')){
//your code goes here
}
//jquery
if($('#save').length){
//your code goes here
}
});
回答by pala?н
You can do this:
你可以这样做:
if($('#save').length > 0){
// Button exists
} else {
// Button is not present in the DOM yet
}