首页 > 解决方案 > 如何使用java脚本隐藏文本并显示另一个文本和整个java脚本代码由php触发?

问题描述

$_SESSION['MSG']="Incorrect email id and password"是红色的。

5 秒后它会隐藏,然后我想在 5 秒后在同一位置以黄色显示稍后再试一次。$_SESSION['try']="Try again later"但它不会隐藏,直到页面没有重新加载或刷新:

<?php
session_start();
if(isset($_SESSION['MSG'])){
echo '<div  id="incorrect">' . $_SESSION['MSG'] .  '</div><script 
type="text/javascript">  
function showIt() {  
document.getElementById("incorrect").style.visibility = "hidden";  
}  
setTimeout("showIt()", 5000); 
</script>';
}
unset ($_SESSION['MSG']); 
session_destroy ();
?>

标签: javascriptphpcss

解决方案


尝试这个

if(isset($_SESSION['MSG'])){
    echo '<div id="incorrect">'.$_SESSION['MSG'].'</div><script type="text/javascript">
    document.getElementById("incorrect").style.cssText = "background: red";
    function showIt() {
        document.getElementById("incorrect").style.visibility = "hidden";  
    }
    setTimeout("showIt()", 5000);
    function showIt() {
        document.getElementById("incorrect").style.cssText = "background: yellow";
        document.getElementById("incorrect").style.visibility = "visible";
        document.getElementById("incorrect").innerHTML = "'.$_SESSION["try"].'";
    }
    </script>';
}

您使用innerHTML来更改 中的消息<div></div>


推荐阅读