dongmi0760 2016-06-19 12:19
浏览 26

用javascript隐藏div

Why doesn't this work. What should i do with the js to hide me the s_login div? pls help

<?php
define("INSTALLING", true);
$hide = true;
if(INSTALLING == true): ?>
    <script type="text/JavaScript">
    <?php if (isset($hide)){if($hide == true){echo "
    document.getElementById('s_login').style.visibility = 'hidden';
    ";}} ?>
    </script>
    <div id="s_login">
        <form action="index.php" method="GET">
            <input type="text" name="s_host" placeholder="MySQL Host" />
            <input type="text" name="s_user" placeholder="MySQL Username"  />
            <input type="password" name="s_password" placeholder="MySQL Password" />
            <input type="submit" name="submit" value="Log In" />
        </form>
    </div>
<?php else: ?>
<?php endif; ?>
  • 写回答

2条回答 默认 最新

  • dpj997991 2016-06-19 12:24
    关注

    Put the hiding element script code inside dom ready or window onload method. Your script is executed first and at that time the dom i.e you s_login element isn't available inside the DOM.

    With the current code check this and you'll get null. I.e you don't have an element at hand which you hide.

    var element = document.getElementById('s_login');
    console.log(element);
    

    if you are using jquery you can do this:

    $(document).on("ready",function(){
       document.getElementById('s_login').visibility = "hidden"
    });
    

    else natively

    window.onload = function(){
       document.getElementById('s_login').visibility = "hidden"
    }
    

    Didn't use DOMContentLoaded Listener as that can have some cross browser concerns.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备