简体   繁体   English

单击链接时隐藏div-PHP

[英]Hide a div when a link is clicked - PHP

Im trying to make a div disappear anytime a link is clicked on php, this is my code 我试图使div在php中单击链接时消失,这是我的代码

<?php
                  echo '<div id="content">
                          <table width="300">
                              <tr>
                                <td>
                               <a href="calendar.php" style="color: red" target="_top">'.$name.'</a>
                                 </td>
                               </tr>
                           </table> </div>';
     //when this is clicked, hide the 'content' div 
    echo '<a href="javascript:;" id="next">next</a>';

?>

this is my script code that is not doing anything for now 这是我的脚本代码,目前暂时不执行任何操作

<script type="text/javascript">
$('#next').live("click",function(){
     $('#content').hide();
});
 </script>

How can I hide the <div id="content"> when the link 'next' is clicked? 单击链接“下一步”时如何隐藏<div id="content"> Any suggestion will help thanks! 任何建议都会有所帮助!

Use this: 用这个:

$(document).ready(function(){
    $('#next').on('click',function(){
        $('#content').hide();
    });
}

Demo: on function 演示: on功能

But! 但! As mentioned in comments, this can not work, if you have version < 1.7. 如注释中所述,如果您的版本<1.7,则此方法将无效。 on function had been added in 1.7 version of jQuery. on功能已在1.7版本的jQuery中添加。

If you have jQuery version < 1.9 you can use live function.: 如果您的jQuery版本低于1.9,则可以使用live功能。:

$(document).ready(function(){
    $('#next').live('click',function(){
         $('#content').hide();
    });
}

Demo: live function 演示: live功能

This function deprecated since 1.7 and it was removed in 1.9. 从1.7开始不推荐使用此功能,在1.9中已将其删除。

If you using jQuery < 1.7, you can use live function, but if not, you should use on , since live is deprecated. 如果使用jQuery <1.7,则可以使用live函数,但如果不使用, 则应使用on ,因为不赞成使用live

put your script in document ready 将脚本放入文档中

$(function() {
 $('#next').live("click",function(){
      $('#content').hide();
 });
})

read more about it here: http://api.jquery.com/ready/ 在这里阅读更多有关它的信息: http : //api.jquery.com/ready/

Try this 尝试这个

<script>
    $(document).ready(function(){
        $(document).on("click","#next", function(){
            $("#content").hide();
        });
    });
</script>

using $(document).ready(function(){ will avoid error in IE 8. 使用$(document).ready(function(){将避免IE 8中的错误。

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

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