onfocus 表单输入更改边框颜色?

发布于 2024-10-10 16:52:34 字数 527 浏览 0 评论 0原文

我试图在用户单击字段时向表单字段添加颜色边框,我擅长 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

毁梦 2024-10-17 16:52:34

使用 onBlur 和 onFocus 添加和减去一个类可能会更清晰。然后在 css 类中,您可以:

.focus {
background: yellow;
color: #000;
border: 1px solid red;
}

检查此处有关边框属性的更多信息。 (对那些讨厌 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:

.focus {
background: yellow;
color: #000;
border: 1px solid red;
}

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/

燃情 2024-10-17 16:52:34

只需使用 css 来设置轮廓颜色,如下所示:

.class {
    outline-color:#FF0;
}

Just use css for outline color like so:

.class {
    outline-color:#FF0;
}
淡淡的优雅 2024-10-17 16:52:34

您可以使用 :focus 伪类 #q:focus {border:red 1px Solid;} 但正如您在此处看到的 http://www.quirksmode.org/css/contents.html ie 7及以下版本不支持。要实现跨浏览器兼容性,您可以使用 jquery 和以下代码

$('#q').focus(function() {
    $('#q').css('border','1px solid red');
});

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

$('#q').focus(function() {
    $('#q').css('border','1px solid red');
});
清风不识月 2024-10-17 16:52:34
input:not([type="button"]):not([type="submit"]):not([type="reset"]):hover, input:not([type="button"]):not([type="submit"]):not([type="reset"]):focus, textarea:hover, textarea:focus, select:hover, select:focus 
{    
    border:2px solid #1b2a44;    
    outline:none;
}
.form-control{
padding:5px;
background-color:#f7f7f7;
border:1px solid red;
}
 <label>TextBox 1:</label><input type="text" class="form-control">
  <label>TextBox 2:</label> <input type="text" class="form-control">

input:not([type="button"]):not([type="submit"]):not([type="reset"]):hover, input:not([type="button"]):not([type="submit"]):not([type="reset"]):focus, textarea:hover, textarea:focus, select:hover, select:focus 
{    
    border:2px solid #1b2a44;    
    outline:none;
}
.form-control{
padding:5px;
background-color:#f7f7f7;
border:1px solid red;
}
 <label>TextBox 1:</label><input type="text" class="form-control">
  <label>TextBox 2:</label> <input type="text" class="form-control">

迷离° 2024-10-17 16:52:34

卡尔-迈克尔·休斯的答案最终对我有用!轮廓颜色是设置焦点“边框”颜色的唯一方法。谢谢!

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!

嗳卜坏 2024-10-17 16:52:34
input:not([type="button"]):not([type="submit"]):not([type="reset"]):hover, input:not([type="button"]):not([type="submit"]):not([type="reset"]):focus, textarea:hover, textarea:focus, select:hover, select:focus {
    border-color: #81256f;
    box-shadow: none;
}

使用这个CSS。这将为您完成这项工作。通过上面的 CSS,我实现了以下输出:
输入图片此处描述

希望这对您有帮助:-)

input:not([type="button"]):not([type="submit"]):not([type="reset"]):hover, input:not([type="button"]):not([type="submit"]):not([type="reset"]):focus, textarea:hover, textarea:focus, select:hover, select:focus {
    border-color: #81256f;
    box-shadow: none;
}

Use this CSS. This will do the job for you. With the above CSS i achieved the following output:
enter image description here

Hope this helped you :-)

吹泡泡o 2024-10-17 16:52:34

我不建议使用这样的内联样式,甚至建议通过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文