HTML 输入 onfocus 和 onblur

发布于 2024-12-28 18:00:39 字数 394 浏览 4 评论 0原文

我试图让输入文本字段在聚焦时周围有一个厚蓝色边框,然后在不再聚焦时默认返回到正常的默认边框。我的一个输入字段的定义如下:

<input onfocus="this.style.border='2px solid #003366';" onblur="this.style.border='';" name="attorneyName" id="attorneyName" type="text" value="John Jackson" size="50" maxlength="30">

当它处于焦点时,它会正确地在它周围放置一个蓝色边框,但是当它不再处于焦点时,它会完全丢失默认的文本字段。未定义任何输入文本字段边框;它们的样式是此类字段的默认样式,我只是想让它在失去焦点后恢复到这种样式。

I am trying to get an input text field to have a thick blue border around it when it is in focus and then to default back to its normal default border when it is no longer in focus. One of my input fields is defined like so:

<input onfocus="this.style.border='2px solid #003366';" onblur="this.style.border='';" name="attorneyName" id="attorneyName" type="text" value="John Jackson" size="50" maxlength="30">

This correctly puts a blue border around it when it is on focus, but when it is no longer in focus, it loses the default text field altogether. None of the input text field borders are defined; their style is the default style for such fields and I would simply like to make it revert to this style after it loses focus.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

匿名的好友 2025-01-04 18:00:40

css 有一个 :focus psuedo 选择器,您可以将样式附加到其中。

input:focus { border:2px solid #036; }

另外,不要忘记使用正确的文档类型。

css has a :focus psuedo selector which you can attach styles to.

input:focus { border:2px solid #036; }

Also, don't forget to use the right doctype. <!DOCTYPE html>

丢了幸福的猪 2025-01-04 18:00:40

不要使用 JavaScript 直接修改 style 属性,而是添加和删除类名,并使用 CSS 定义类:

.newInputClass {
    border: 2px solid #003366;
}

并且在 HTML 中:

<input onfocus="this.className = 'newInputClass';" onblur="this.className = '';" name="attorneyName" id="attorneyName" type="text" value="John Jackson" size="50" maxlength="30">

Instead of directly modifying the style attribute with the JavaScript, instead add, and remove, a class-name, and use CSS to define the class:

.newInputClass {
    border: 2px solid #003366;
}

And, in the HTML:

<input onfocus="this.className = 'newInputClass';" onblur="this.className = '';" name="attorneyName" id="attorneyName" type="text" value="John Jackson" size="50" maxlength="30">
陌路终见情 2025-01-04 18:00:40

这是因为当您聚焦或“模糊”时,您实际上是从文本框中删除整个样式,而不是将其恢复为默认值。

<input onfocus="this.style.border='2px solid #003366';" onblur="this.style.border='';" name="attorneyName" id="attorneyName" type="text" value="John Jackson" size="50" maxlength="30">

将模糊语句设置为具有以下值。你想要一个边框样式: inset;

border-style: inset; 
border-width: 2px; 

希望这有帮助

This is because when you are focusing out or "blurring", you are actually removing the entire style from the text box, not reverting it back to its default.

<input onfocus="this.style.border='2px solid #003366';" onblur="this.style.border='';" name="attorneyName" id="attorneyName" type="text" value="John Jackson" size="50" maxlength="30">

Set your blur statement to have the following values. You want a border-style: inset;

border-style: inset; 
border-width: 2px; 

Hope this helps

纵情客 2025-01-04 18:00:40

试试这个:

onblur="this.style.border='none';"

或者更好地避免内联样式并按照 @David 的建议交换类。

Try this:

onblur="this.style.border='none';"

or even better avoid the inline styles and swap a class as @David suggests.

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