访问网站时搜索框有效。

时间:2014-02-11 13:21:56

标签: javascript jquery html css

我有一个搜索框,我想在客户访问我的网页时激活它。

我的HTML是:

<form class="form-search top-search" action="Search.aspx" onsubmit="return validateSearch(this);" method="GET">
<input type="text" class="input-medium search-query yui-ac-input" name="ss" id="ss" placeholder="Search and find it fast" title="Search and find it fast" onblur="if (this.value == '') { this.value = 'Search and find it fast'; }" onfocus="if ((this.value == 'Search and find it fast')||(this.value == 'Search and find it fast')) { this.value = ''; }" onkeypress="if ((this.value == 'Please enter a keyword or item #')||(this.value == 'Search and find it fast')) { this.value = ''; } else{ document.getElementsByName('ss')[0].style.color='black'; }" autocomplete="off">
<input type="submit" title="Search" class="btn btn-orange SearchButton" value="Search">
</form>

我只想将用户默认为该字段,就像亚马逊默认将您置于搜索框中一样。

4 个答案:

答案 0 :(得分:3)

您可以使用HTML5 autofocus属性:

<input type="text" autofocus>

答案 1 :(得分:2)

您可以使用.focus()聚焦输入

$(function () {
    $('#ss').focus();
});

DEMO

答案 2 :(得分:1)

纯Javascript解决方案,适用于IE6 +和任何其他浏览器。

将它放在正文结束标记

之前
    <script type="text/javascript">

   var obj = document.getElementById("ss");
   obj.focus(); 

  </script>

答案 3 :(得分:0)

您可以在页面加载后使用这些选项中的任何一个。

$('#ss').focus();

$('#ss').select();

$('#ss').trigger('focus');

$('#ss').trigger('click');

例如:

$(document).ready(function(){
     $('#ss').trigger('click');
});