這是我的代碼:需要幫助的JavaScript增添類
<form>
<input type="text" id="username" onblur="class();" name="username">
</form>
<script>
function class() {
var username = document.getElementById('username');
if (username == "") {
//Now here I want to add a class
}
}
</script>
現在我想的時候不會給出用戶名值添加類。我怎樣才能做到這一點?
'用戶名== 「」'是不正確的,因爲一個元素不能爲空字符串。 – Xufox
你的意思是說'input'是空的? –
注意:你不能在函數聲明中使用'class'作爲函數的名字(你上面有什麼),因爲名字必須有* Identifier *,* Identifier *是* * IdentifierName *而不是* ReservedWord *」。 'class'是一個保留字(他們實際上是在ES2015中開始做的)。 –