gpt4 book ai didi

Javascript函数取消隐藏元素似乎没有调用var

转载 作者:行者123 更新时间:2023-12-01 05:39:36 24 4
gpt4 key购买 nike

尝试在我的网站标题上显示一条消息,该消息应根据日期每天更改。使用下面的脚本,但似乎它没有调用 var。因为它什么也没显示,我不知道哪一个错过了这一点。

<script type="function/javascritp">
function unhide(id) {
var element = document.getElementById(id);
element.className = element.className.replace('hidden', '');
}

switch((new Date()).getDay()) {
case 0:
// Sunday
unhide('sun');
break;
case 1:
// Monday
unhide('mon');
break;
case 2:
// Tuesday
unhide('tues');
break;
case 3:
// Wednesday
unhide('wed');
break;
case 4:
// Thursday
unhide('thurs');
break;
case 5:
// Friday
unhide('fri');
break;
case 6:
// Saturday
unhide('sat');
break;
};
</script>
</head>

<body>

<Header>
<div id="sun" class="hidden">Today is Sunday</div>
<div id="mon" class="hidden">Today is Monday</div>
<div id="tues" class="hidden">Today is Tuesday</div>
<div id="wed" class="hidden">Today is Wednesday</div>
<div id="thurs" class="hidden">Today is Thursday</div>
<div id="fri" class="hidden">Today is Friday</div>
<div id="sat" class="hidden">Today is Saturday</div>
</header>
</body>

我正在使用CSS代码,

.hidden {
display: none;
}

最佳答案

它必须是 .hidden 而不是 CSS 中的 hidden;你忘了添加句点 . ,其余的看起来都不错

function unhide(id) {
var element = document.getElementById(id);
element.className = element.className.replace('hidden', '');
}

switch((new Date()).getDay()) {
case 0:
// Sunday
unhide('sun');
break;
case 1:
// Monday
unhide('mon');
break;
case 2:
// Tuesday
unhide('tues');
break;
case 3:
// Wednesday
unhide('wed');
break;
case 4:
// Thursday
unhide('thurs');
break;
case 5:
// Friday
unhide('fri');
break;
case 6:
// Saturday
unhide('sat');
break;
};
.hidden{display:none}
<Header>
<div id="sun" class="hidden">Today is Sunday</div>
<div id="mon" class="hidden">Today is Monday</div>
<div id="tues" class="hidden">Today is Tuesday</div>
<div id="wed" class="hidden">Today is Wednesday</div>
<div id="thurs" class="hidden">Today is Thursday</div>
<div id="fri" class="hidden">Today is Friday</div>
<div id="sat" class="hidden">Today is Saturday</div>
</Header>

关于Javascript函数取消隐藏元素似乎没有调用var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31501096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com