【问题标题】:Passing an Ajax-generated URL to a JQuery tab in web page将 Ajax 生成的 URL 传递到网页中的 JQuery 选项卡
【发布时间】:2021-04-27 12:11:24
【问题描述】:

我在处理 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);
        });
  })
}

【问题讨论】:

  • 这是一个相当长的描述,可能是一个简单的问题。您能否将您的代码(和解释)简化为minimal, reproducible example,以便我们更容易准确地理解问题出在哪里?
  • 您好,您需要获取整个 html 页面吗?为什么不使用.load() 方法。
  • response中的链接有问题吗?请向我们展示console.log(response) 的示例,以便我们查看链接的组成。
  • 你真的不应该在另一个文档中插入一个完整的 HTML 文档。充其量它会产生一个无效的 HTML 文档。在最坏的情况下,它会破坏页面(例如,如果新插入的文档包含已经在父文档中的 ID)。如果您向我们展示您尝试插入的这些文档之一的完整内容,将会很有帮助。
  • 我认为 iframe 会不必要地复杂。

标签: javascript html jquery ajax jquery-ui-tabs


【解决方案1】:

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

Ajax

// ...
// 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 选项卡,...:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-25
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2012-09-20
    • 2013-03-10
    • 1970-01-01
    相关资源
    最近更新 更多