javascript - 来自链接的简单 JQuery UI 对话框

标签 javascript html jquery dialog anchor

我已经对此进行了几个小时的试验,但我仍然感到困惑。

我试图在单击链接([a] 标记)时打开一个 JQuery UI 对话框(模态),从链接的 href 获取对话框窗口的内容。

到目前为止,我已经(从各个地方收集到)其中 testb.html 是一个简单的 html 片段:

<div><p>Some text</p><p>more text</p</div>

这个想法是,当单击 anchor (链接)时,对话框中会出现 testb.html 的内容。

为什么这行不通???

David(70 岁的前阿尔茨海默氏症前程序员,几乎没有 HTML 经验)

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery test</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
  <script>
    $("a.modal").click(function(e) {
      e.preventDefault();
      $(".container").load(this.href).dialog("open");
    });
  </script>
</head>

<body>

  <div class="container"></div>
  <p><a href="testb.html" class="modal">Click!</a></p>

</body>

</html>

最佳答案

您可以使用此代码:

    <!doctype html>
    <html lang="en">
    
    <head>
        <title>Title</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="//code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">
    </head>
    
    <body>
    
        <div class="container">
        <p><a href="javascript:void(0)" data-get="testb.html" class="modal">Click!</a></p>
</div>
        <div id="dialog" title="Basic dialog"></div>
        
        <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
        <script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
    
        <script>
            $('.modal').on('click', function () {
                var data = $(this).attr('data-get')
                $('#dialog').html(data)
                $("#dialog").dialog()
            });
        </script>
    </body>
    
    </html>

关于javascript - 来自链接的简单 JQuery UI 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69789629/

相关文章:

javascript - 检测何时由 Android 浏览器的屏幕键盘触发调整大小事件

javascript - 如何在较小的空间中放置大图片

javascript - 是否可以从文件输入多个元素中删除单个值?

javascript - 工作 jQuery 代码在条件内不起作用

jquery - 如何有条件地执行 jquery 验证?

javascript - 从 Javascript 中的数组值生成有效输出

javascript - 使用 Javascript 检测 IE 中的高级隐私设置

javascript - TS错误: Type 'unknown' is not assignable to type 'FunctionComponent<any> due to withRouter

javascript - 在使用 jQuery 创建的元素上使用 jQuery

javascript - 如何使用重置按钮删除选择选项标签中的背景颜色?