鍍金池/ 問答/HTML/ 函數(shù)寫成立即執(zhí)行函數(shù)或放到其他函數(shù)內(nèi)部,就執(zhí)行失敗

函數(shù)寫成立即執(zhí)行函數(shù)或放到其他函數(shù)內(nèi)部,就執(zhí)行失敗

本人在做一個導航首頁,做搜索時,想用百度搜索智能提示功能,遇到了一個奇怪的問題。代碼如下:

<body>
    <input type="text" id="q">
    <ul id="show"></ul>
    <script>
        function solve(data) { //得到百度API返回的數(shù)據(jù)
            var Show = document.getElementById('show');
            var html = "";

            if (data.s.length) {
                Show.style.display = 'block';
                var len = data.s.length;
                for (var i = 0; i < len; i++) { //逐條顯示
                    html += '<li><a target="_blank"  + data.s[i] + '">' + data.s[i] + '</a></li>';
                }
                Show.innerHTML = html;
            } else {
                Show.style.display = "none";
            }
        }

        var oQ = document.getElementById('q'),
            Show = document.getElementById('show');

        oQ.onkeyup = function() { //當有鍵盤事件的時候
            if (this.value != "") {
                var oScript = document.createElement("script"); //創(chuàng)建一個script標簽,準備引入資源
                oScript.src = 'http://suggestion.baidu.com/su?wd=' + this.value + '&cb=solve';
                document.body.appendChild(oScript);
            } else {
                Show.style.display = "none";
            }
        }
    </script>
</body>

當前代碼可執(zhí)行,可以運行出正確結(jié)果。但是當我將其放到其他函數(shù)內(nèi)部,就執(zhí)行失敗。
代碼如下:

<body>
    <input type="text" id="q">
    <ul id="show"></ul>
    <script>
        (function() {
            function solve(data) { //得到百度API返回的數(shù)據(jù)
                var Show = document.getElementById('show');
                var html = "";

                if (data.s.length) {
                    Show.style.display = 'block';
                    var len = data.s.length;
                    for (var i = 0; i < len; i++) { //逐條顯示
                        html += '<li><a target="_blank"  + data.s[i] + '">' + data.s[i] + '</a></li>';
                    }
                    Show.innerHTML = html;
                } else {
                    Show.style.display = "none";
                }
            }

            var oQ = document.getElementById('q'),
                Show = document.getElementById('show');

            oQ.onkeyup = function() { //當有鍵盤事件的時候
                if (this.value != "") {
                    var oScript = document.createElement("script"); //創(chuàng)建一個script標簽,準備引入資源
                    oScript.src = 'http://suggestion.baidu.com/su?wd=' + this.value + '&cb=solve';
                    document.body.appendChild(oScript);
                } else {
                    Show.style.display = "none";
                }
            }
        })()
    </script>
</body>

有大神知道原因嗎?

回答
編輯回答
朕略傻

走在路上,突然想到了這是什么問題。。

var oScript = document.createElement("script"); 
oScript.src = 'http://suggestion.baidu.com/su?wd=' + this.value + '&cb=solve';
document.body.appendChild(oScript);

script在標簽插入DOM中才向baidu請求數(shù)據(jù),請求到數(shù)據(jù),會調(diào)用solve回調(diào)函數(shù),但當前全局作用域中沒有solve函數(shù),所以會執(zhí)行失敗。。

2017年7月30日 11:38