AJAX 选项卡运行良好。这部分非常简单。但是,让 AJAX UI Dialog 模式窗口触发链接并不成功。
对此的任何帮助将不胜感激。
AJAX 选项卡运行良好。这部分非常简单。但是,让 AJAX UI Dialog 模式窗口触发链接并不成功。
对此的任何帮助将不胜感激。
没有比那个人更容易的了。试试这个:
<?xml version="1.0" encoding="iso-8859-1"?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>
<style>
.loading { background: url(/img/spinner.gif) center no-repeat !important}
</style>
</head>
<body>
<a class="ajax" href="http://www.google.com">
Open as dialog
</a>
<script type="text/javascript">
$(function (){
$('a.ajax').click(function() {
var url = this.href;
// show a spinner or something via css
var dialog = $('<div style="display:none" class="loading"></div>').appendTo('body');
// open the dialog
dialog.dialog({
// add a close listener to prevent adding multiple divs to the document
close: function(event, ui) {
// remove div with all data and events
dialog.remove();
},
modal: true
});
// load remote content
dialog.load(
url,
{}, // omit this param object to issue a GET request instead a POST request, otherwise you may provide post parameters within the object
function (responseText, textStatus, XMLHttpRequest) {
// remove the loading class
dialog.removeClass('loading');
}
);
//prevent the browser to follow the link
return false;
});
});
</script>
</body>
</html>
请注意,您无法从本地远程加载,因此您必须将其上传到服务器或其他任何地方。另请注意,您无法从外部域加载,因此您应该将链接的 href 替换为托管在同一域上的文档(这是解决方法)。
干杯
为了避免在多次单击链接时添加额外div
的 s,并避免在使用脚本显示表单时出现问题,您可以尝试使用 @jek 代码的变体。
$('a.ajax').live('click', function() {
var url = this.href;
var dialog = $("#dialog");
if ($("#dialog").length == 0) {
dialog = $('<div id="dialog" style="display:hidden"></div>').appendTo('body');
}
// load remote content
dialog.load(
url,
{},
function(responseText, textStatus, XMLHttpRequest) {
dialog.dialog();
}
);
//prevent the browser to follow the link
return false;
});`
//正确格式化
<script type="text/Javascript">
$(function ()
{
$('<div>').dialog({
modal: true,
open: function ()
{
$(this).load('mypage.html');
},
height: 400,
width: 600,
title: 'Ajax Page'
});
});
只是对nicktea答案的补充。此代码加载远程页面的内容(不重定向到那里),并在关闭它时进行清理。
<script type="text/javascript">
function showDialog() {
$('<div>').dialog({
modal: true,
open: function () {
$(this).load('AccessRightsConfig.htm');
},
close: function(event, ui) {
$(this).remove();
},
height: 400,
width: 600,
title: 'Ajax Page'
});
return false;
}
</script>
对于可以打开指向不同页面的对话框的多个元素,前两个答案都不适用于我。
这感觉像是最干净的解决方案,只在加载时创建一次对话框对象,然后使用事件适当地打开/关闭/显示:
$(function () {
var ajaxDialog = $('<div id="ajax-dialog" style="display:hidden"></div>').appendTo('body');
ajaxDialog.dialog({autoOpen: false});
$('a.ajax-dialog-opener').live('click', function() {
// load remote content
ajaxDialog.load(this.href);
ajaxDialog.dialog("open");
//prevent the browser from following the link
return false;
});
});
好奇 - 为什么“没有比这更容易”的答案(上面)不起作用?看起来合乎逻辑? http://206.251.38.181/jquery-learn/ajax/iframe.html
我结合了几个答案,并想出了下面是在模式对话框中打开外部 url 的 JQuery 代码。
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.min.js" integrity="sha256-wS9gmOZBqsqWxgIVgA8Y9WcQOa7PgSIX+rPA0VL2rbQ=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js" integrity="sha256-PsB+5ZEsBlDx9Fi/GXc1bZmC7wEQzZK4bM/VwNm1L6c=" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<body>
<a href="https://wikipedia.com/" class="test">comment #1</a>
<br>
<a href="https://ebay.com/" class="test">comment #2</a>
<br>
<a href="https://ask.com/" class="test">comment #3</a>
<br>
<a class="ajax" href="https://api.github.com">Open github</a>
<br>
<a class="ajax" href="https://code.google.com/p/html5security/wiki/CrossOriginRequestSecurity">Open code google</a>
<br>
<a class="ajax" href="https://enable-cors.org/">Open enable-cors</a>
<br>
<div id="somediv" title="this is a dialog" style="display:none;">
<iframe id="thedialog" width="350" height="350"></iframe>
</div>
<script type="text/javascript">
$(document).ready(function () {
$(".test").click(function ()
{
var url = $(this).attr("href");
return openDialogwithiFrame(url);
});
});
</script>
<script type="text/javascript">
$(function (){
$('a.ajax').click(function() {
var url = this.href;
return openDialogwithiFrame(url);
});
});
function openDialogwithiFrame(url)
{
$("#thedialog").attr('src', url);
$("#somediv").dialog({
width: 400,
height: 450,
modal: true,
close: function () {
$("#thedialog").attr('src', "about:blank");
}
});
return false;
}
function openDialogwithoutiFrame(url)
{
// show a spinner or something via css
var dialog = $('<div style="display:none" class="loading"></div>').appendTo('body');
// open the dialog
dialog.dialog({
// add a close listener to prevent adding multiple divs to the document
close: function(event, ui) {
// remove div with all data and events
dialog.remove();
},
modal: true
});
// load remote content
dialog.load(
url,
//{}, omit this param object to issue a GET request instead a POST request, otherwise you may provide post parameters within the object
function (responseText, textStatus, XMLHttpRequest) {
// remove the loading class
dialog.removeClass('loading');
}
);
//prevent the browser to follow the link
return false;
}
</script>
</body>
</html>
该代码有两个功能。
Header set Access-Control-Allow-Origin “*” //Replace domain of host sites separated by comma for *
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS"
Header always set Access-Control-Allow-Headers "content-type "
Header always set Access-Control-Allow-Credentials "true"
<a href="javascript:void(0)" onclick="$('#myDialog').dialog();">
Open as dialog
</a>
<div id="myDialog">
I have a dialog!
</div>