简体   繁体   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>

` `

I'm wanting it to open a popup with the popup title of Message.我想让它打开一个弹出标题为消息的弹出窗口。 and with the text of test.和测试文本。 I've tried so many things to fix this but I can't.我已经尝试了很多方法来解决这个问题,但我做不到。 I've come to the conclusion that it's not selecting the elements properly.我得出的结论是它没有正确选择元素。

Here is what I suggest:这是我的建议:

  • The first thing I noticed is that there is no async on the left of function on your document ready.我注意到的第一件事是,您的文档准备就绪的函数左侧没有async Try adding async and see if it solves the problem尝试添加async并查看它是否解决了问题

  • Or may by try to combine that 2 script tags into one或者可以尝试将这 2 个脚本标签合并为一个

  • If it still doesn't work, try logging it out with console.log() or regular alert() on your value, see if it's even return something at all.如果它仍然不起作用,请尝试在您的值上使用console.log()或常规alert()将其注销,看看它是否甚至返回了一些东西。 Maybe that $("#p") doesn't need the first index to be accessible because it is an id, not a class也许$("#p")不需要访问第一个索引,因为它是一个 id,而不是一个类

conclusion that it's not selecting the elements properly结论是它没有正确选择元素

That's not correct, your code is working fine.这是不正确的,您的代码工作正常。 Just that the dialog is not opened because autoOpen is set to false .只是对话框没有打开,因为autoOpen设置为false I tried this code, hope it helps:我试过这段代码,希望它有所帮助:

(also I don't think you need Promise) (我也不认为你需要承诺)

 <: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>

Just one thing not related to your error though just informing you so you will remember this, this code is actually not right只有一件事与你的错误无关,虽然只是通知你所以你会记住这一点,这段代码实际上是不正确的

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

The await keyword is only valid inside async functions within regular JavaScript code. await 关键字仅在常规 JavaScript 代码中的异步函数内有效。

Just rewrite right way... hope it helps!只需以正确的方式重写...希望对您有所帮助!

Any questions, just ask and I'll answer in the comments.有什么问题,尽管问,我会在评论中回答。

 <:--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