2014-02-11 72 views
1

我有一个搜索框,当客户访问我的网页时,我想使其处于活动状态。访问网站时搜索框处于活动状态。

我的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> 

我只是想给用户你默认进入该领域,怎么样亚马逊默认到他们的搜索框。

+0

什么是积极的手段? –

+0

如同在页面加载时默认进入搜索字段。但只限于主页。在产品页面上没有意义,因为如果用户在产品页面上,那么他们可能找到了他们想要的东西。听起来很合逻辑,我想。 –

回答

2

您可以使用.focus()来集中输入

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

DEMO

+0

有没有办法只在我的主页上加载页面?从我所看到的,亚马逊只有当你访问主页时才会这样做。 –

+1

你只把代码放在主页上比... – epascarello

+0

似乎不工作在FF v27 – MLeFevre

3

可以使用HTML5 autofocus属性:

<input type="text" autofocus> 
+2

请注意,自动对焦属性为HTML5 – MLeFevre

+0

虽然我在我的页面上使用Modernizr.js来提供HTML5/CSS3支持,但我不确定它是否受支持。 –

+0

看看浏览器的支持:http://www.wufoo.com/html5/attributes/02-autofocus.html – chris

0

您可以使用其中任一选项后页面加载。

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

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

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

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

例如:

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

纯JavaScript的解决方案,工作IE6 +和其他任何浏览器。

体结束标记之前将这个

<script type="text/javascript"> 

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

    </script> 
相关问题