gpt4 book ai didi

javascript - 我做错了什么?

转载 作者:行者123 更新时间:2023-11-28 16:34:30 25 4
gpt4 key购买 nike

实时代码:http://jsfiddle.net/vy4nY/

我正在关注this challenge ,但我遇到了一些问题。我试图使“电子邮件地址”框仅在单击该复选框时出现。我做错了什么?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>JavaScript Challenges</title>
<style type="text/css">
#emailpara {visibility:hidden;}
</style>
</head>
<body>
<form action="">
<fieldset>

<legend>Email subscriptions</legend>

<p id="subscribepara">
<label>
<input type="checkbox" name="subscribe" id="subscribe">
Yes! I would like to receive the occasional newsletter via email.
</label>
</p>

<p id="emailpara">
<label>
Email Address:
<input type="text" name="email" id="email">
</label>
</p>

</fieldset>
</form>
<script type="text/javascript">
document.getElementById('subscribe').onclick = (document.getElementById('subscribe').checked ? (document.getElementById('emailpara').style.visibility = 'visible') : (document.getElementById('emailpara').style.visibility = 'hidden'));
</script>
</body>
</html>

最佳答案

onclick 处理程序应该是一个函数。使用:

document.getElementById('subscribe').onclick = function() {
document.getElementById('emailpara').style.visibility = this.checked ? 'visible' : 'hidden';
}

关于javascript - 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4561208/

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