代码之家  ›  专栏  ›  技术社区  ›  coder

为什么要获取SCRIPT5007:SCRIPT5007:无法设置inter中未定义或空引用的属性“disabled”

  •  -1
  • coder  · 技术社区  · 6 年前

    SCRIPT5007:SCRIPT5007:无法设置未定义或空引用的属性“disabled”。sql.html(6,1)

    这是我的密码

    <html>
        <head>
            <script>
                if (!window.openDatabase){
                    alert("Sorry your browser dosent support WebSQL")
                    document.getElementById("input").disabled = true
                    document.getElementById("button").disabled = true
                } else {
                    var db = openDatabase("mydb", 1.0, "mydb", 2*1024*1024)
                    function execute(){
                        db.transaction(function (t){
                            t.executeSql(document.getElementById("input").value)
                            console.log(document.getElementById("input").value)
                        })
                    }
                }
            </script>
        </head>
        <body id="body">
            <textarea id="input"></textarea>
            <button onclick="execute()" id="button">Execute SQL</button>
        </body>
    </html>
    

    我的代码怎么了?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Zhi Lv    6 年前

    您可以将代码放入onload事件中。加载对象后发生onload事件。

    请修改您的代码如下:

    <head>
        <script>
            function pageload() { 
                if (!window.openDatabase) {
                    alert("Sorry your browser dosent support WebSQL")
                    document.getElementById("input").disabled = true
                    document.getElementById("button").disabled = true
                } else {
                    var db = openDatabase("mydb", 1.0, "mydb", 2 * 1024 * 1024)
                    function execute() {
                        db.transaction(function (t) {
                            t.executeSql(document.getElementById("input").value)
                            console.log(document.getElementById("input").value)
                        })
                    }
                }
            }
        </script>
    </head>
    <body id="body" onload="pageload();">
        <textarea id="input"></textarea>
        <button onclick="execute()" id="button">Execute SQL</button>
    </body>