I have this example.
我有这个例子。
When I click, a dynamic content load via Ajax. In the loaded page there is a link, that could close the div. It works, but when the div is closed (with hide()), the click on link don't reopen loaded page.
当我点击时,通过Ajax加载动态内容。在加载的页面中有一个链接,可以关闭div。它工作正常,但当div关闭时(使用hide()),点击链接不会重新打开加载的页面。
Why? How can I create a button, that show/hide a div loaded with Ajax?
为什么?如何创建一个按钮,显示/隐藏装有Ajax的div?
Calling page :
致电页面:
<script type="text/javascript">
function getAjax(divdett,pageload) {
$(function(){$(divdett).load(pageload);});}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>click</title>
</head>
<body>
<a href="javascript:getAjax('#siteloader','jquery_divajax.htm');">click</a>
<div id="siteloader"></div>
</body>
Included page :
包含的页面:
<script type="text/javascript">
function delAjax(divdett) {
$(divdett).hide();
}
</script>
OK!
<a id="#chiudi" href="#" onclick="delAjax('#siteloader');">chiudi</a>
4 个解决方案
#1
1
In your "close" button, you call hide()
.
在“关闭”按钮中,调用hide()。
Change your getAjax
function to this :
将getAjax函数更改为:
function getAjax(divdett,pageload) {
$(function(){$(divdett).load(pageload).show();});}
I just added .show()
to ensure the div is visible even if it has been made hidden before.
我刚刚添加了.show()以确保div是可见的,即使它之前已被隐藏。
Note that I respected the original construct but this is much too complex : no need to call $(function
when you can simply do
请注意,我尊重原始构造,但这太复杂了:无需调用$(函数就可以了
function getAjax(divdett,pageload) {
$(divdett).load(pageload).show();}
If you want to have only one button, you may remove the one of the second file and change the getAjax function to
如果您只想拥有一个按钮,则可以删除第二个文件中的一个并将getAjax函数更改为
function getAjax(divdett,pageload) {
var div = $(divdett);
if (div.is(':visible')) div.hide();
else div.load(pageload).show();
}
#2
0
I would using an unobtrusive approach, separating the html from the javascript (jQuery) code, and like @dystroy very well pointed out, just show the div before the ajax request is made to make sure it's visible:
我会使用一种不引人注意的方法,将html与javascript(jQuery)代码分开,并且非常好地指出@dystroy,只需在执行ajax请求之前显示div以确保它是可见的:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>click</title>
<script type="text/javascript">
$(function() {
$('#clickme').on('click', function(event) {
$('#siteloader').show().load('jquery_divajax.htm');
event.preventDefault();
});
$('#siteloader').on('click', '#chiudi', function(event) {
$('#siteloader').hide();
event.preventDefault();
});
});
</script>
</head>
<body>
<a id="clickme" href="">click</a>
<div id="siteloader"></div>
</body>
#3
0
well my answer may help you... giving you the scenario...
好吧,我的回答可以帮助你......给你一个场景......
you can use if else in response of ajax request
你可以使用if else来响应ajax请求
$.ajax(
{
url:"ChkLogin.php",
type:"POST",
dataType: 'json',
data: {usernm: unm, passwrd: pass },
success:function(myoutput)
{
$(":hidden").val(myoutput.srno);
if(myoutput.flag=="1")
{
window.location="chat.php";
}
else
{
$("#msg").html("Invalid Login");
}
}
});
is response in 1 then do this else do this
是1的响应然后做这个做这个
like....
喜欢....
if(myoutput.flag=="1")
{
$("#div").hide(4000);
}
else
{
$("#div").show(4000);
}
here 4000 are ms(milliseconds) takes to hide or show the DIV] Hope this will help you
这里4000是ms(毫秒)隐藏或显示DIV]希望这会帮助你
#4
0
I think it doesn't work because HTML DOM already loaded. For dynamic binding you need to use function .bind()
or .live()
(Documentation for bind()).
我认为它不起作用,因为HTML DOM已经加载。对于动态绑定,您需要使用函数.bind()或.live()(bind()的文档)。
For example:
例如:
$( "#foo" ).bind( "click", function() {
$( this ).hide(); // or show();
});
#1
1
In your "close" button, you call hide()
.
在“关闭”按钮中,调用hide()。
Change your getAjax
function to this :
将getAjax函数更改为:
function getAjax(divdett,pageload) {
$(function(){$(divdett).load(pageload).show();});}
I just added .show()
to ensure the div is visible even if it has been made hidden before.
我刚刚添加了.show()以确保div是可见的,即使它之前已被隐藏。
Note that I respected the original construct but this is much too complex : no need to call $(function
when you can simply do
请注意,我尊重原始构造,但这太复杂了:无需调用$(函数就可以了
function getAjax(divdett,pageload) {
$(divdett).load(pageload).show();}
If you want to have only one button, you may remove the one of the second file and change the getAjax function to
如果您只想拥有一个按钮,则可以删除第二个文件中的一个并将getAjax函数更改为
function getAjax(divdett,pageload) {
var div = $(divdett);
if (div.is(':visible')) div.hide();
else div.load(pageload).show();
}
#2
0
I would using an unobtrusive approach, separating the html from the javascript (jQuery) code, and like @dystroy very well pointed out, just show the div before the ajax request is made to make sure it's visible:
我会使用一种不引人注意的方法,将html与javascript(jQuery)代码分开,并且非常好地指出@dystroy,只需在执行ajax请求之前显示div以确保它是可见的:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>click</title>
<script type="text/javascript">
$(function() {
$('#clickme').on('click', function(event) {
$('#siteloader').show().load('jquery_divajax.htm');
event.preventDefault();
});
$('#siteloader').on('click', '#chiudi', function(event) {
$('#siteloader').hide();
event.preventDefault();
});
});
</script>
</head>
<body>
<a id="clickme" href="">click</a>
<div id="siteloader"></div>
</body>
#3
0
well my answer may help you... giving you the scenario...
好吧,我的回答可以帮助你......给你一个场景......
you can use if else in response of ajax request
你可以使用if else来响应ajax请求
$.ajax(
{
url:"ChkLogin.php",
type:"POST",
dataType: 'json',
data: {usernm: unm, passwrd: pass },
success:function(myoutput)
{
$(":hidden").val(myoutput.srno);
if(myoutput.flag=="1")
{
window.location="chat.php";
}
else
{
$("#msg").html("Invalid Login");
}
}
});
is response in 1 then do this else do this
是1的响应然后做这个做这个
like....
喜欢....
if(myoutput.flag=="1")
{
$("#div").hide(4000);
}
else
{
$("#div").show(4000);
}
here 4000 are ms(milliseconds) takes to hide or show the DIV] Hope this will help you
这里4000是ms(毫秒)隐藏或显示DIV]希望这会帮助你
#4
0
I think it doesn't work because HTML DOM already loaded. For dynamic binding you need to use function .bind()
or .live()
(Documentation for bind()).
我认为它不起作用,因为HTML DOM已经加载。对于动态绑定,您需要使用函数.bind()或.live()(bind()的文档)。
For example:
例如:
$( "#foo" ).bind( "click", function() {
$( this ).hide(); // or show();
});