onfocus 表单输入更改边框颜色?
我试图在用户单击字段时向表单字段添加颜色边框,我擅长 html 和 javascript,还有一点 php,但我的 css 实际上很差。表格等的代码如下。如果有人可以指导或帮助我,我将非常感激。 代码:
<form id="search" action="http://www.bkslap.com/search/results.php" id="cse-search-box">
<input name="q" type="text" id="q" autocomplete="on" size="56" style="color:#ccc;" maxlength="128" id="q"
onblur="this.value = this.value || this.defaultValue; this.style.color = '#ccc';"
onfocus="this.value=''; this.style.color = '#9933FF';"
value="Search" />
</form>
有什么想法吗?
I'm trying to add a colour border to a form field when a user clicks on the field, I'm good at html and javascript, with a bit of php, but my css is actually quite poor. The code for the form etc is below. I would really appreciate if anyone could direct or help me.
CODE:
<form id="search" action="http://www.bkslap.com/search/results.php" id="cse-search-box">
<input name="q" type="text" id="q" autocomplete="on" size="56" style="color:#ccc;" maxlength="128" id="q"
onblur="this.value = this.value || this.defaultValue; this.style.color = '#ccc';"
onfocus="this.value=''; this.style.color = '#9933FF';"
value="Search" />
</form>
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 onBlur 和 onFocus 添加和减去一个类可能会更清晰。然后在 css 类中,您可以:
检查此处有关边框属性的更多信息。 (对那些讨厌 w3schools 的人表示歉意,但对于此类参考来说,这是一个合理的地方)。
http://www.cssdrive.com/index.php/examples/exampleitem/ focus_pseudo_class/
It'd probably be cleaner to add and subtract a class with the onBlur and onFocus. Then in the css class you could have:
Check here for more information on the border properties. (with apologies to those who hate w3schools, but it's a reasonable place for this type of reference).
http://www.cssdrive.com/index.php/examples/exampleitem/focus_pseudo_class/
只需使用 css 来设置轮廓颜色,如下所示:
Just use css for outline color like so:
您可以使用
:focus
伪类#q:focus {border:red 1px Solid;}
但正如您在此处看到的 http://www.quirksmode.org/css/contents.html ie 7及以下版本不支持。要实现跨浏览器兼容性,您可以使用 jquery 和以下代码you can use the
:focus
pseudoclass#q:focus {border:red 1px solid;}
but as you can see here http://www.quirksmode.org/css/contents.html it's not supported by ie 7 and below. To achieve cross browser compatibility you can use jquery and the following code卡尔-迈克尔·休斯的答案最终对我有用!轮廓颜色是设置焦点“边框”颜色的唯一方法。谢谢!
The answer from Carl-Michael Hughes is what finally worked for me! outline-color is the only way to set the focus "border" color. Thanks!
使用这个CSS。这将为您完成这项工作。通过上面的 CSS,我实现了以下输出:
希望这对您有帮助:-)
Use this CSS. This will do the job for you. With the above CSS i achieved the following output:
Hope this helped you :-)
我不建议使用这样的内联样式,甚至建议通过 javascript/jquery 连接事件,但是...
您可以使用 object.style.borderColor 设置背景颜色。颜色只是字体颜色。
速记中的 CSS 标记就是“border”,或者具体来说,如果您想从 css 设置边框颜色,则它是“border-color”。在 javascript 中,转向 this.style.borderColor。
I don't recommend using inline style like this and would even recommend wiring up the events via javascript/jquery but...
You can set background color with object.style.borderColor. Color is only the font color.
The css markup in shorthand is just 'border' or specifically if you want to set border color from css it is 'border-color'. In javascript that turns to this.style.borderColor.