根据要执行的if语句切换DOM元素

时间:2018-10-31 11:43:16

标签: javascript html

我正在研究一个JavaScript项目,该项目根据您的不同而打招呼。 在一天中的时间是实时的。

例如,如果小时数少于12,则表示早上好。但是我缺少一件事,我不知道如何添加。

我希望显示一个按钮以及可能不同的div,具体取决于哪个 if / if else语句被执行。例如,如果是早晨,则尝试显示“煮咖啡”按钮,如果是下午,则显示“打开电视”等。

问题是我不知道如何执行此操作。

<!DOCTYPE html>
<html>
<head>
    <title>Greeting | Test</title>
</head>
<body align="center">

<style>

</style>

<script>

var Name = prompt("Hello! What is your name?");

var d = new Date();
var hour = d.getHours();
var min = d.getMinutes();
var hourAd = hour - 12;


if(hour < 12){ //target if statement to toggle button
   document.write("Good Morning , " + Name + "." +
                   "<br /> It is currently " +
                    hour + ":" + min + " Am" );
                    //button code
}

else if(hour > 12){
        document.write("Good Afternoon , " + Name + "." +
        "<br /> It is currently " +
        hourAd + ":" + min + " Pm" );
}

else if(hour > 19){
        document.write("Good Evening , " + Name + "." +
        "<br /> It is currently " +
        hourAd + ":" + min + " Pm " );
}
</script>

<!--execute when if statement is true-->
<button id="coffee">Brew Coffee</button>
<button id="tv">Turn on TV</button>
<button id="dinner">Let's make dinner</button>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

首先,您需要使用style="display:none"document.getElementById("your display id").style.display = "block";隐藏所有div,您可以概述以下工作示例:

var Name = prompt("Hello! What is your name?");

var d = new Date();
var hour = d.getHours();
var min = d.getMinutes();
var hourAd = hour - 12;


if(hour < 12){ //target if statement to toggle button
   document.write("Good Morning , " + Name + "." +
                   "<br /> It is currently " +
                    hour + ":" + min + " Am" );
                    //button code
  document.getElementById("coffee").style.display = "block";                  
}

else if(hour > 12){
        document.write("Good Afternoon , " + Name + "." +
        "<br /> It is currently " +
        hourAd + ":" + min + " Pm" );
        document.getElementById("tv").style.display = "block"; 
}

else if(hour > 19){
        document.write("Good Evening , " + Name + "." +
        "<br /> It is currently " +
        hourAd + ":" + min + " Pm " );
        document.getElementById("dinner").style.display = "block"; 
}
 <!DOCTYPE html>
<html>
<head>
    <title>Greeting | Test</title>
</head>
<body align="center">

<style>

</style>


<!--execute when if statement is true-->
<button id="coffee" style="display:none">Brew Coffee</button>
<button id="tv" style="display:none">Turn on TV</button>
<button id="dinner" style="display:none">Let's make dinner</button>

答案 1 :(得分:0)

我的意思是简单的jquery / javascript可以做到;只需添加或删除类或CSS。 也许要显示:无; css取决于时间

jquery示例(单击时更改按钮颜色)

ERROR:tensorflow:Couldn't understand architecture name '.50_%image_size%'
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/content/tensorflow-for-poets-2/scripts/retrain.py", line 1326, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "/content/tensorflow-for-poets-2/scripts/retrain.py", line 976, in main
    model_info = create_model_info(FLAGS.architecture)
  File "/content/tensorflow-for-poets-2/scripts/retrain.py", line 923, in create_model_info
    raise ValueError('Unknown architecture', architecture)
ValueError: ('Unknown architecture', '.50_%image_size%')
    $( "button#myBtnID" ).click(function() {
    $(this).toggleClass( "mycss_class" );
    });
    //you can do addClass() or removeClass() too; see jquery api

 
.mycss_class {
    color: white;
    background:green;
}