我正在尝试使用onFocus和onBlur属性设置搜索框的样式,但我没有得到预期的结果。这是我的 HTML 代码
<form action="index.php" method="post" class="search">
<input name="searchword" id="mod_search_searchword" maxlength="20"
type="text" size="30" value="Search..."
onblur="if(this.value=='') {this.value='Search...'; this.className='search-default';}
elseif(this.value!='' && this.value!='Search...') {this.className='search-userinput';}"
onfocus="if(this.value=='Search...') {this.value=''; this.className='search-userinput';}
else {this.className='search-userinput';}" class="search-userinput">
<input type="hidden" name="option" value="com_search">
<input type="hidden" name="task" value="search">
</form>
如果我没有为 onBlur 属性设置条件,则 onFocus 位可以工作,但我需要两者都工作。我有什么解决方案?
这将
起作用:
<script>
function setb(obj)
{
if(obj.value=='')
{obj.value='Search...'; obj.className='search-default';}
else if(obj.value!='' && obj.value!='Search...')
obj.className='search-userinput';
}
function setf(obj)
{
if(obj.value=='Search...')
{obj.value=''; obj.className='search-userinput';}
else
obj.className='search-userinput';
}
</script>
<form action="index.php" method="post" class="search">
<input name="searchword" id="mod_search_searchword" maxlength="20"
type="text" size="30" value="Search..."
onblur="setb(this)"
onfocus="setf(this)" class="search-userinput">
<input type="hidden" name="option" value="com_search">
<input type="hidden" name="task" value="search">
</form>
而且我们没有elseif
! 那是else if
.