我试图调用一个 javascript 函数来读取 currnet 按钮的 innerHTML 并使用 if 语句来确定要在屏幕上显示的项目。第一次点击时它会进行所需的更改,但第二次点击时不会执行任何操作。
function signup() {
var test = document.getElementById('link-user').innerHTML;
if (test = 'Sign Up') {
document.getElementById('signup-name-first').style.display = 'block';
document.getElementById('signup-name-last').style.display = 'block';
document.getElementById('signup-email').style.display = 'block';
document.getElementById('signup-password').style.display = 'block';
document.getElementById('br1').style.display = 'block';
document.getElementById('br2').style.display = 'block';
document.getElementById('br3').style.display = 'block';
document.getElementById('btn-sub').innerHTML = 'Sign Up';
document.getElementById('link-user').innerHTML = 'Existing User';
document.getElementById('link-forgot').style.display = 'none';
} else {
document.getElementById('signup-name-first').style.display = 'none';
document.getElementById('signup-name-last').style.display = 'none';
document.getElementById('signup-email').style.display = 'none';
document.getElementById('signup-password').style.display = 'none';
document.getElementById('br1').style.display = 'none';
document.getElementById('br2').style.display = 'none';
document.getElementById('br3').style.display = 'none';
document.getElementById('btn-sub').innerHTML = 'Log In';
document.getElementById('link-user').innerHTML = 'Sign Up';
document.getElementById('link-forgot').style.display = 'block';
}
}
<section id="win-login" class="win-login eds-win-pop eds-ani-popup">
<form class="">
<br />
<input id="signup-name-first" type="text" placeholder="First Name" name="namefirst" class="input-txt eds-input-pop eds-ani-popup" />
<br id="br1" class="eds-input-pop" />
<input id="signup-name-last" type="text" placeholder="Last Name" name="namelast" class="input-txt eds-input-pop eds-ani-popup" />
<br id="br2" class="eds-input-pop" />
<input id="signup-email" type="text" placeholder="E-Mail Address" name="email" class="input-txt eds-input-pop eds-ani-popup" />
<br id="br3" class="eds-input-pop" />
<input type="text" placeholder="User Name" name="nameuser" class="input-txt" />
<br />
<br />
<input type="password" placeholder="Password" name="password" class="input-txt" />
<br />
<br />
<input id="signup-password" type="password" placeholder="Re-Enter Password" name="passwordconf" class="input-txt eds-input-pop eds-ani-popup" />
<br />
<button id="btn-sub" type="submit" class="btn-skyblu" action="">Login</button>
</form>
<br>
<a id="link-user" class="link-small" onclick="signup();">New User</a>
<br />
<a id="link-forgot" class="link-small" onclick="pwreset();">Forgot Password</a>
</section>
最佳答案
在 bool 表达式中使用 ==
而不是 =
。像这样:
if (test == 'Sign Up') {
//your code
}
有关比较运算符的更多信息: http://www.w3schools.com/js/js_comparisons.asp
关于javascript - 使用带有 innerHTML 的 if 语句来替换内容,我的方法不起作用......完全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39923989/