简体   繁体   English

ajax:使用ajax关闭后如何重新打开div

[英]ajax: how to reopen a div after closing using ajax

I call a page with ajax in a div. 我在div中用ajax调用页面。 when the page opens, there is a "close "button to hide the div. 当页面打开时,有一个“关闭”按钮可以隐藏div。 After clicking the close button, I need to refresh the page to reopen the div. 单击关闭按钮后,我需要刷新页面以重新打开div。 What should I do to open and close the div as much as I like without refreshing the page? 我应该怎么做才能在不刷新页面的情况下尽可能多地打开和关闭div?

code: 码:

<script>
$(document).ready(function(){   

$('#close').click(function() {
  $('#show_options').hide();
  $(window).unload( function () { alert("Bye now!"); } );
});


});

</script>

 <a href="#" id="close">Close</a> <div id="show_options" style="position:absolute;     width:500px; padding:10px; left: 246px; top: 41px; z-index:900;"></div>My selection: <span     id="select-result" style="font-family:Arial, Helvetica, sans-serif;font-    size:10pt;color:#444;">leeg</span> </div>



<a ONClick="xmlhttpPost('selected_output_team.php', 'options', 'show_options'); return         false; " id="show_options"> edit</a>

I haven't tested this, but at the "close" anchor, try this: 我尚未对此进行测试,但是在“接近”锚点处,尝试以下操作:

<a href="#" id="close" onclick="closeDiv();">Close</a>
... stuff ...
<a href="#" id="open" onclick="openDiv();">Open</a>

Once you have that, you can, instead of removing the div, simply remove (and readd) its contents: 一旦有了它,您就可以删除它的内容,而不是删除div:

function closeDiv()
{
    document.getElementById("show_options").innerHTML = "";
}
function openDiv()
{
    document.getElementById("show_options").innerHTML = "...whatever...";
}

There are other ways of course, but this is one of them. 当然还有其他方法,但这就是其中一种。 Note that I haven't tested the code. 请注意,我尚未测试代码。

使用toggle()代替hide()或show()

I am not an expert but if you do not want to hide or show the div tag with animation, you can simply change the visibility of the div tag using CSS. 我不是专家,但是如果您不想隐藏或显示带有动画的div标签,则可以使用CSS来简单地更改div标签的可见性。 You can have the following functions to close and open the div tag: 您可以具有以下功能来关闭和打开div标签:

function close() {
    document.getElementById("show_options").style.display = "none";
}
function open() {
    document.getElementById("show_options").style.display = "block";
}

Nice thing about CSS is that you don't have to regenerate content, therefore I think it should work faster and your code could be shorter. CSS的好处是您不必重新生成内容,因此我认为它应该运行得更快,并且您的代码可能会更短。

I'm not really sure that I understand your question. 我不确定我是否理解您的问题。 However, if you simply wants the close-link to execute the javascript function, and nothing else, replace the "#" with "javascript:;" 但是,如果您只是想让close-link执行javascript函数,而别无其他,请将“#”替换为“ javascript :;” in the link-tag. 在链接标记中。 The anchor might give you some unexpected behavior at times. 锚有时可能会给您一些意外的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM