javascript - 将 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);
});
})
}
解决方案
我设法设计了一个解决方案。对于那些感兴趣的人,这里是代码的重要部分。
阿贾克斯
// ...
// 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 选项卡,...:
推荐阅读
- c# - 如何正确反序列化 Json DateTimeOffset?
- ruby - 如何将字符串拆分为 n 个子字符串?
- php - 显示错误:上传路径似乎无效
- c# - 有没有办法禁用所有 WPF 应用程序的缩放,即使 Windows 在每个监视器或系统范围内都有一定的缩放比例?
- cuda - Cuda - nvcc - 没有可在设备上执行的内核映像。问题是什么?
- html - 在 Chrome 中编辑 CSS 规则时不要突出显示 HTML 区域
- mysql - 如何在执行 SQL 转储时替换某些列的值
- css - 如何在不重置 CSS 的情况下添加 Bootstrap?
- c++ - 是否有必要为具有另一个类 B 的数据成员的类 A 重载赋值运算符和复制构造函数?
- image - 如何在 Flask 的同一页面上查看预测和图像?