因此,我有一个由4个输入组成的表单,用户名、密码、电子邮件和姓名。
我厌倦了为它们中的每一个创建javascript函数,因为每个输入都是文本输入,当用户单击输入框时,我会让它将输入的背景更改为不同的颜色。
以下是我的编码方式:
表单输入:
<input type="text" name="username" id="usernameInput" onclick="changeUsername(); return false;" onblur="changeUsernameback(); return false;">
<input type="text" name="password" id="passwordInput" onclick="changePassword(); return false;" onblur="changePasswordback(); return false;">
另外两种表单是相同的,只是名称不同,id和javascript函数不同。
我的Javascript:
function changeUsername() {
document.getElementById( usernameInput ).style.background= #FFFF00 ;
}
function changeUsernameBack() {
document.getElementById( passwordInput ).style.background= #FFFF00 ;
}
其他三个只是为自己的特定id设置的。
在创建CSS时,我必须为所有4个输入创建不同的ID。
我想知道的是:有没有一种方法可以让我只能制作一个CSS id和一个javascript函数来更改所有输入?因为我知道,当你只对所有函数使用一个函数时,javascript会试图一次更改所有函数。。
我在想
document.getElementById( inputText +[i]).style.background= #FFFF00 ;
然后,当我给每个输入一个id时,我可以在页面上自动增加它们,比如input1、input2、input3等。
但这似乎不起作用?也许我编码错了?请帮忙。。