从加载的php文件中隐藏div

时间:2013-03-07 11:36:56

标签: php ajax

我在页面上有一个div,当用户点击链接时,会使用Ajax将PHP文件加载到其中。这工作正常,但我想要做的是在加载的PHP文件中有一个onclick事件,以便能够在页面上隐藏相同的div。

3 个答案:

答案 0 :(得分:1)

如果我理解正确,你想用一个通过ajax加载到div中的按钮来隐藏div。

您可以使用.on

在您的ajax文件中添加buttonclose

<button class="close">Close</button>

在你的页面中发出请求添加这个jQuery

$(function(){

    $(document).on('click', 'button.close', function(){
        $(this).parents('div').hide();
    });

});

显然你可能需要改变.parents()调用中的选择器以匹配你的父div,你总是可以在div中添加一个类并将其作为目标。

答案 1 :(得分:0)

试试这个:

1. Add a button or link in loaded php file content
2. Write a onclick event to hide the div
3. As the php file content is loaded through AJAX, it will not bind to document, 
   So you need to use on or delegate function of jQuery to work it properly

参考:http://api.jquery.com/on/http://api.jquery.com/delegate/

答案 2 :(得分:0)

在你加载的php文件中写一些相应的javascript。例如:

调用ajax之前的HTML:

<div id="my_container"></div>

<script type="text/javascript">
    //using jQuery
    $("#hide_parent").live('click', function(){
        $("my_container").hide();
    });
</script>

在你的php脚本中:

<a href="#" id="hide_parent">Hide me away</a>