检查按钮是否存在

Joh*_*ava 7 html javascript jquery jsp button

我有一个.js文件,由几个.jsp页面调用.我在这个特定的.js文件中需要做的是只有在.jsp页面中存在"保存"按钮时才调用某个函数.如何检查按钮是否存在?目前在.js文件中按钮的方式是这样的:$('button [id = save]')

如何检查是否存在这样的按钮?希望有人可以提供建议.谢谢.

raj*_*wat 14

尝试这样的事情

    $(document).ready(function(){
        //javascript
        if(document.getElementById('save')){
            //your code goes here
        }

        //jquery
        if($('#save').length){
            //your code goes here
        }
    });
Run Code Online (Sandbox Code Playgroud)


pal*_*aѕн 4

你可以这样做:

if($('#save').length > 0){
    // Button exists
} else {
    // Button is not present in the DOM yet

}
Run Code Online (Sandbox Code Playgroud)