Javascript 如何从Google搜索框中删除默认Google搜索值?
我试图使用一个小jQuery来“隐藏”谷歌搜索框中的初始值,当你点击该框时,我遗漏了一些东西 以下是搜索框代码:Javascript 如何从Google搜索框中删除默认Google搜索值?,javascript,jquery,Javascript,Jquery,我试图使用一个小jQuery来“隐藏”谷歌搜索框中的初始值,当你点击该框时,我遗漏了一些东西 以下是搜索框代码: <div id="search_box"> <form action="http://mfrp.ehclients.com/search_results" id="cse-search-box"> <div> <input type="hidden" name="cx" value="017425724926122041548:1
<div id="search_box">
<form action="http://mfrp.ehclients.com/search_results" id="cse-search-box">
<div>
<input type="hidden" name="cx" value="017425724926122041548:1ccj3d-e1-s" />
<input type="hidden" name="cof" value="FORID:9" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" id="q" name="q" value="Search..." size="31" />
</div>
</form>
<script type="text/javascript" src="http://www.google.com/cse/brand?form=cse-search-box&lang=en"></script>
</div>
以下是jQuery:
<script type="text/javascript">
$('#q').click(function() {
if($(this).val() == 'Search...')
$(this).val('');
});
</script>
$('#q')。单击(函数(){
如果($(this).val()=='Search…')
$(this.val(“”);
});
唯一的问题是,它不起作用。这是你的电话号码
如果能帮我解决这个问题,我将不胜感激
谢谢
福雷斯
你被谷歌伪造了。。。文本框有一个背景,看起来好像有值。。。您应该在开发工具上检查这一点
所以只要试着在模糊中移除它
$('#q').blur(function(){
$(this).css('background','none');
if(this.value == '') {
this.value = this.defaultValue;
}
}).focus(function() {
if(this.value == this.defaultValue) {
this.value = '';
}
});
当您的代码需要在DOM元素可用时运行,因此
$(“#q”)
找到要绑定的id=“q”
元素。这意味着您的脚本需要在页面中所需的元素之后运行(例如,
),或者在DOM完全就绪时运行,如下所示:
$(function() {
$('#q').click(function() {
if($(this).val() == 'Search...')
$(this).val('');
});
});
$(function() {
$('#q').focus(function() {
if($(this).val() == 'Search...') $(this).val('');
}).blur(function() {
if($(this).val() == '') $(this).val('Search...');
});
});
如果您不这样做,并且选择器找不到任何元素,那么就没有可以运行的东西,这会绑定该处理程序
我想你想要的也是相反的,看起来是这样的:
$(function() {
$('#q').click(function() {
if($(this).val() == 'Search...')
$(this).val('');
});
});
$(function() {
$('#q').focus(function() {
if($(this).val() == 'Search...') $(this).val('');
}).blur(function() {
if($(this).val() == '') $(this).val('Search...');
});
});
如果“搜索…”是空的,并且有人通过依赖和而不是在框外单击,这将把它放回框中。尝试console.log($(This).val())并查看它给了您什么。当我将您的javascript代码粘贴到firebug中并运行它时,框会做它应该做的事情。编辑:在黑暗中拍摄,尝试$(文档)。准备(函数(){/*代码*/}嗨,卢瓦特,你是对的!我添加了(文档)。准备好了,它工作了。嗨,尼克,你的61.5K分很好。谢谢你的解决方案。