首页 > 解决方案 > 将 Ajax 生成的 URL 传递到网页中的 JQuery 选项卡

问题描述

我在处理 Ajax 编码的 URL 时遇到问题。

我正在通过 Ajax 脚本查询数据库 (Solr),将输出发送到网页(仅在我家用计算机上的本地主机网络服务器上本地提供)。

当我单击 Ajax 生成的链接 (URL) 时,它们会在另一个浏览器选项卡中打开,而不是在源网页中打开。

对于原型,手动添加到我的网页的硬编码 URL 可以正确显示,在 JQuery“文档”选项卡中的同一网页中打开:

$(init);
function init(){
  $(function() {
    $("#tabs").tabs();
  });

  $('#testURLs a').on('click', function (event) {
    event.preventDefault();

    // My JQuery tabs: 0: Search; 1: Documents 
    $( "#tabs" ).tabs({ active: 1 });
    $.ajax({
      method: "GET",
      // url: "http://127.0.0.1:8080/test/d1.html",
      url: this.href,
      data: {}
      }).done(function(response) {
          $("#documents_tab_docs").html(response);
        });
  })
}

标签: javascripthtmljqueryajaxjquery-ui-tabs

解决方案


我设法设计了一个解决方案。对于那些感兴趣的人,这里是代码的重要部分。

阿贾克斯

// ...
// Localserver: http-server --cors /mnt/Vancouver/
//...
var output = '<div id="title"><h3>
    <a class="docTitle" href="http://127.0.0.1:8081/programming/datasci/solr/test/'
    + doc.filename + '"><b>' + doc.title + '</b></a></h3>';
// ...
return output;
//...
//----------------------------------------
//...
init: function () {
  $(document).on('click', 'a.docTitle', function () {
      var $this = $(this);
      var url = this.href;

      $.ajax({
      method: "GET"
      }).done(function(response) {
          // Use numbered (not named) tabs: 
          $( "#tabs" ).tabs({ active: 1 });
          $("#iframe_docs").attr("src", url);
          });

      return false;
  });
}

HTML

<!-- ... -->
<div id="documents_tab" class="tab">
  <!-- <h2>Documents</h2> -->
  <ul>
    <!-- Documents, etc. from the Search tab will appear here. -->
    <!-- https://stackoverflow.com/questions/40903065/how-to-embed-iframe-with-external-website -->
    <div id="documents_tab_docs"></div>
      <iframe id="iframe_docs" src="">
      </iframe>
  </ul>
</div>
<!-- ... -->

CSS

#iframe_docs {
  border-style: none;
  width: 100%;
  /* height: 100%; */
  /* vh : viewport height */
  /*   https://stackoverflow.com/questions/5272519/how-do-you-give-iframe-100-height */
  /*   https://stackoverflow.com/questions/5867985/full-screen-iframe-with-a-height-of-100 */
  height: 100vh;
  background: #f8f9f9;
}

这是该实现的视频(注意:虚拟数据;原始,开发代码):

https://www.youtube.com/watch?v=sLL9ooqi_xU

这里的相关背景(我的),回复:JQuery 选项卡,...:


推荐阅读