如何在输入ONFOCUS外显示文字?

时间:2011-07-01 13:21:05

标签: php javascript html

我有一个这样的表格:

<form action="search.php" class='search' method='GET'>
<input 
    type="text/submit"
    class="home_search_area"
    value="Search"
    name="search" 
    onblur="if(value=='') value = 'Search'" 
    onfocus="if(value=='Search') value = ''"
 /></form>

现在我的表格在焦点上隐藏了“搜索”字样。我想在textarea onfocus外面显示文本“按Enter键开始搜索”。我怎么能这样做?

6 个答案:

答案 0 :(得分:3)

在输入字段下添加一个元素:

<div id="tips"></div>

添加到onfocus事件:

onfocus="if(value=='Search') value = ''; document.getElementById('tips').innerHTML = 'Press enter...'"

答案 1 :(得分:2)

在输入元素附近添加第二个DIV或SPAN元素并为其指定一个ID并在其中显示您的文本

<script>
function test(){
if(this.value=='Search'){
this.value = '';
document.getElementById("show").innerHTML = "Press enter to start searching";
}

}
</script>
<form action="search.php" class='search' method='GET'>
<input 
    type="text/submit"
    class="home_search_area"
    value="Search"
    name="search" 
    onblur="if(value=='') value = 'Search'" 
    onfocus="test(this);"
 />
<div id=show></div>
</form>

答案 2 :(得分:0)

我只是将一个空div放在一边,并且在onfocus中有$("#hintdiv").innerHTML = 'Press enter to start searching'

答案 3 :(得分:0)

使用id,div就像上面的回答一样,功能类似

function MyOnFocusFunc() {
    document.getElementById(yourinputid).value = '';
    document.getElemenbById('hintdiv').innerHTML = 'Press enter to start searching';
}

答案 4 :(得分:0)

<span id="showtext"></span>
<form action="search.php" class='search' method='GET'>
<input 
type="text/submit"
class="home_search_area"
value="Search"
name="search"  
onfocus="document.getElementById('showtext').innerHTML = 'Press enter  to start searching'"
/></form>

答案 5 :(得分:0)

试试这个;

<form action="search.php" class='search' method='GET'>
<input 
    type="text/submit"
    class="home_search_area"
    value="Search"
    name="search" 
    onblur="if(value=='') value = 'Search'" 
    onfocus="if(value=='Search') value = ''"
    id="myInput"
 />
<div id="ShowThis" style="display:none;">Press enter to start searching</div>
</form>

然后是Jquery

$('#myInput').focus( function() {
    $('#ShowThis').css('display','block');
});