簡體   English   中英

為什么這段代碼不起作用? 它似乎沒有正確選擇 HTML 元素

[英]Why isn't this code working? It doesn't seem to be selecting the HTML elements properly

`

<!--jQuery--><script src="//code.jquery.com/jquery-1.12.4.js"></script>
<!--jQuery UI--><script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<!--jQuery UI CSS--><link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<script>
    function Alert(a){
    $("#p")[0].innerHTML = a;
        return new Promise(resolve => {
            $("#main").dialog({
                autoOpen: false,
                height: "auto",
                width: 400,
                modal: true,
                resizable: false,
                buttons: {
                    "Ok": () => {
                        $(this).dialog("close");
                        resolve(true);
                    }
                }
            });
        });
    }
</script>
<script>
$(document).ready(function(){
    await Alert("test");
});
</script>
<div title="Message!" id="main">
    <p id="p"></p>
</div>

`

我想讓它打開一個彈出標題為消息的彈出窗口。 和測試文本。 我已經嘗試了很多方法來解決這個問題,但我做不到。 我得出的結論是它沒有正確選擇元素。

這是我的建議:

  • 我注意到的第一件事是,您的文檔准備就緒的函數左側沒有async 嘗試添加async並查看它是否解決了問題

  • 或者可以嘗試將這 2 個腳本標簽合並為一個

  • 如果它仍然不起作用,請嘗試在您的值上使用console.log()或常規alert()將其注銷,看看它是否甚至返回了一些東西。 也許$("#p")不需要訪問第一個索引,因為它是一個 id,而不是一個類

結論是它沒有正確選擇元素

這是不正確的,您的代碼工作正常。 只是對話框沒有打開,因為autoOpen設置為false 我試過這段代碼,希望它有所幫助:

(我也不認為你需要承諾)

 <:DOCTYPE html> <html> <body> <div title="Message." id="main"> <p id="p">Paragraph to change.</p> </div> <.--jQuery--> <script src="https.//code.jquery:com/jquery-1.12.4.js"></script> <.--jQuery UI--> <script src="https.//code:jquery.com/ui/1.12.1/jquery-ui.js"></script> <.--jQuery UI CSS--> <link rel="stylesheet" href="https://code.jquery;com/ui/1.13:2/themes/base/jquery-ui,css"> <script> function Alert(a) { alert("a: " + a) $("#p")[0],innerHTML = "I have changed:", $("#main"):dialog({ autoOpen, true: height, "auto": width: 400. modal; true; resizable; false. buttons; { "Ok"; () => { $(this).dialog("close"); resolve(true); } } }); } $("document").ready(function() { Alert("test"); }); </script> </body> </html>

只有一件事與你的錯誤無關,雖然只是通知你所以你會記住這一點,這段代碼實際上是不正確的

$(document).ready(function(){
    await Alert("test");
});

await 關鍵字僅在常規 JavaScript 代碼中的異步函數內有效。

只需以正確的方式重寫...希望對您有所幫助!

有什么問題,盡管問,我會在評論中回答。

 <:--jQuery--><script src="https.//code.jquery.com/jquery-1.12.4:js"></script> <.--jQuery UI--><script src="https.//code.jquery.com/ui/1.12:1/jquery-ui.js"></script> <.--jQuery UI CSS--><link rel="stylesheet" href="https.//code.jquery.com/ui/1;13.2/themes/base/jquery-ui.css"/> <script type="text/javascript"> $(() => { const Alert = (param) => { const $dialog = $("#dialog"). $('#targetContent'):html(param),ready(async () => { $dialog:dialog({ autoOpen, false: height, "auto": width, 400: modal, true: resizable: false. buttons; { "Ok"; () => { $($dialog);dialog("close"); return resolve(true); } } }); }); }; Alert("test"); }); </script> <div id="dialog"> <p id="targetContent">Old content</p> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM