显示或隐藏使用php和javascript生成的id的div

时间:2013-04-09 04:50:23

标签: php javascript

我试图隐藏或显示基于php函数的div id的div。我无法使其发挥作用,请帮助我。

使用Javascript:

<script>
showOrHide(id) {
    var elem=getElementById(id);
    if(elem.style.visibility="hidden")
        elem.style.visibility="visible";
    else
        elem.style.visibility="hidden";
}
</script>

PHP脚本:

<?php
function display_link($link_id,$upvote_array,$downvote_array,$divid) {
?>
    <a href="javascript:showOrHide(<?php echo $divid; ?>)">More links</a>
    <div id="<?php echo $divid ?>" style="visibility:hidden;">
    </div>
<?php } ?>

3 个答案:

答案 0 :(得分:4)

这是您的功能的清理版本。

function showOrHide(id) {

  var elem = document.getElementById(id);

  elem.style.visibility = (elem.style.visibility === 'hidden')? 'visible' : 'hidden';
}

答案 1 :(得分:0)

<script>
function showOrHide(id){
 var elem = getElementById(id);
 if(elem.style.visibility=="hidden"){
  elem.style.visibility="visible";
 } else {
  elem.style.visibility="hidden";
 }
}
</script>
<?php
function display_link($link_id,$upvote_array,$downvote_array,$divid)
{
?>
<a href="javascript:showOrHide('<?php echo $divid; ?>')">More links</a>
<div id="<?php echo $divid ?>" style="visibility:hidden;">
</div>

1 - visibility =='hidden'

2 - 失踪;在visibility =“visible”和=“hidden”之后

3 - href =“javascript:showOrHide('')” - 在javascript函数中缺少''

答案 2 :(得分:-1)

试试这个

 <script>
    function showOrHide(id){
     var elem = document.getElementById(id);

  elem.style.visibility = (elem.style.visibility === 'hidden')? 'visible' : 'hidden';
    }

    </script>
    <?php
    function display_link($link_id,$upvote_array,$downvote_array,$divid)
    {
    ?>
    <a  href="javascript:showOrHide(<?php echo $divid; ?>)">More links</a>
    <div id="<?php echo $divid ?>" style="display:hidden;">
    </div>
    <?php
    }
    ?>