【问题标题】:Opening a link inside a div in the modal在模态的 div 内打开链接
【发布时间】:2019-10-24 02:00:57
【问题描述】:

我有一个模式,其中有 2 个<a> 标签。当用户点击链接时,链接应该在同一个modal的同一个div中打开,并且不在tab中

为此我尝试过:

$(document).ready(function() {
  $("#help_modal a").each(function() {
    $("#help_modal").load($(this).attr("href"));
  });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<div class="container">
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    Open modal
  </button>

  <!-- The Modal -->
  <div class="modal" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">

        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Modal Heading</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>

        <!-- Modal body -->
        <div class="modal-body" id="help_modal">
          <a href="https://google.com" class="btn btn-block btn-lg btn-primary">Open Google</a>
          <a href="https://stackoverflow.com" class="btn btn-block btn-lg btn-secondary">Open Stackoverflow</a>
        </div>

        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
        </div>

      </div>
    </div>
  </div>

</div>

但是链接在同一个标​​签页中打开...

更新

我也尝试了this answer,但在this comment 之后它也对我不起作用。

【问题讨论】:

    标签: jquery html hyperlink bootstrap-4 html-object


    【解决方案1】:

    回复你的comment

    如果您在运行JSFiddle 时检查控制台,它会显示,

    拒绝显示 'Opening a link inside a div in the modal' 在一个框架中,因为祖先违反了以下内容安全性 政策指令:“frame-ancestors 'self'”。

    这意味着,

    由于设置了内容安全策略,禁止在 IFRAME 中显示该内容。托管 stackoverflow.com 的 Web 服务器配置为向响应对象添加 HTTP 标头。具体来说,他们将 Content-Security-Policy 标记设置为 frame-ancestors 'self'。您无法使用 IFRAME 将他们的页面嵌入到您自己的页面中。

    感谢TimmyB's answer


    回复你的question

    Learner's answer 也是一个不错的选择。 (+1)

    但我更喜欢有不同的 iframe(s) 并通过按钮显示它们。

    $(document).ready(function() {
      $(".modal_button_example_com_self").click(function() {
        $('.modal_button_self').hide();
        $('#example_com').attr("style", "");
      });
    
      $(".modal_button_example_net_self").click(function() {
        $('.modal_button_self').hide();
        $('#example_net').attr("style", "");
      });
    
      $(".close_self").click(function() {
        $('.modal_button_self').attr("style", "");
        $('#example_com').hide();
        $('#example_net').hide();
      });
    
    });
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    
    <div class="container">
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
            Open modal
        </button>
    
      <!-- The Modal -->
      <div class="modal" id="myModal">
        <div class="modal-dialog">
          <div class="modal-content">
    
            <!-- Modal Header -->
            <div class="modal-header">
              <h4 class="modal-title">Modal Heading</h4>
              <button type="button" class="close close_self" data-dismiss="modal">&times;</button>
            </div>
    
            <!-- Modal body -->
            <div class="modal-body" id="help_modal">
              <button class="modal_button_self modal_button_example_com_self btn btn-block btn-lg btn-primary">Open Example.com</button>
              <button class="modal_button_self modal_button_example_net_self btn btn-block btn-lg btn-secondary">Open Example.net</button>
    
              <iframe id="example_com" style="display: none;" src="https://example.com/" width="100%" height="50%" seamless></iframe>
              <iframe id="example_net" style="display: none;" src="https://example.net/" width="100%" height="50%" seamless></iframe>
    
            </div>
    
            <!-- Modal footer -->
            <div class="modal-footer">
              <button type="button" class="btn btn-danger close_self" data-dismiss="modal">Close</button>
            </div>
    
          </div>
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      我设法用一些 javascript 和 html 'object' 标签来实现你想要的:

      我用 toReplace 变量的内容替换您的模态正文的内容,其中包含一个 html 'object' 元素,用于嵌入外部链接。

      let el = document.getElementById("test");
      let elToReplace = document.getElementById("help_modal");
      let toReplace = "<object type=\"text/html\" data=\"https://gnu.org/\" width=\"100%\" height=\"600px\" style=\"overflow:auto;border:5px ridge blue\"> </object>";
      
      function replaceInner() {
          elToReplace.innerHTML = toReplace;
      }
      
      el.addEventListener("click", () => { replaceInner(); }, false);
      

      在此处查看工作代码笔:https://codepen.io/marc-simplon/pen/WBVmYW

      【讨论】:

      • 当我尝试https://stackoverflow.com/questions/56513343/opening-a-link-inside-a-div-in-the-modal 而不是https://gnu.org/ 时,它不起作用。 JSFiddle
      • 除了“object”或“iframe”外,我不知道任何其他嵌入网站的方法。也许你不能(如果网站不允许)?
      猜你喜欢
      • 1970-01-01
      • 2020-06-26
      • 2023-03-11
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      相关资源
      最近更新 更多