简体   繁体   English

突出显示网络表单上的“当前”文本框

[英]Highlighting the “current” textbox on a web form

I have a data entry web app where the control that has focus is given a yellow background color (and when the user navigates away, it turns back to white). 我有一个数据输入Web应用程序,其中具有焦点的控件具有黄色背景色(当用户导航离开时,它变回白色)。 The script to do this came from I-don't-know-where, and it breaks in IE7 causing all sorts of problems, like drop downs not working (the script was designed to work on textboxes and drop downs, or at least it was implemented with that in mind), and it works in ie6. 执行此操作的脚本来自I-not-know-where,它在IE7中中断,导致各种问题,例如下拉菜单无法正常工作(该脚本旨在在文本框和下拉菜单上运行,或者至少可以在下拉菜单中使用)的实现考虑到了这一点),并且可以在ie6中使用。

Some of my users have been switched to Ie7 without my being informed, and the rest will go to ie7 at some future time. 我的一些用户已在不通知我的情况下切换到IE7,其余用户将在将来的某个时间转到IE7。 So, I'd like to implement a friendlier solution. 因此,我想实施一个更友好的解决方案。 I really like jquery and am already using it on the app for a variety of things. 我真的很喜欢jquery,并且已经在应用程序中使用它进行各种操作。 Also, it has been suggested that cross browser support may/will eventually be important on the intranet. 另外,已经建议跨浏览器支持在Intranet上可能/最终将很重要。

What I would prefer to avoid is manually adding a bunch of onblur="SomeMethod()" (or something similar) to the controls (There must be 600+ in the app). 我更希望避免的是向控件中手动添加一堆onblur =“ SomeMethod()”(或类似的东西)(应用程序中必须有600+)。 In fact, if I tell my boss this he's probably going to throw something at me. 实际上,如果我告诉老板,他可能会向我扔东西。

I am already using JQuery in the application. 我已经在应用程序中使用了JQuery。 Where it is used function calls are explicit, and are all called in onblur. 使用函数的地方是显式的,并且全部在onblur中调用。

Currently, I am of the mind to do something like this: 目前,我很想做这样的事情:

$(document).ready (function( 
    $(':text').focus(function() 
    { 
        //Do Highlighting
    }

    $(':text').blur(function()
    {
        //Good bye highlighting
    }
)
  1. Am I on he right track? 我在他的正确轨道上吗? Is onblur my best option? onblur是我最好的选择吗? Is there a better way? 有没有更好的办法?
  2. the other onblur functions show/hide child fields based on the value of the parent. 其他onblur函数根据父级的值显示/隐藏子级字段。 I realize I am not providing code, but am I setting myself up for any conflicts? 我意识到我没有提供代码,但是我是否准备好应对任何冲突?

Use jQuery's blur() and focus() methods. 使用jQuery的blur()和focus()方法。

Also, I think you mean to REMOVE highlighting with your blur function (blur means the user has clicked off of the element in question). 另外,我认为您的意思是使用模糊功能删除突出显示(模糊表示用户已单击有问题的元素)。 Use focus() to turn on the highlighting. 使用focus()打开突出显示。

$(document).ready (function() {
    $(':text').focus(function() { 
        $(this).addClass('highlight');
    });
    $(':text').blur(function() { 
        $(this).removeClass('highlight');
    });
});

There seems to be a workaround that makes the :focus pseudo-class work in IE6/7. 似乎有一种解决方法可以使:focus伪类在IE6 / 7中工作。 I haven't used it myself but I think it's quite a established solution: 我自己没有使用过,但我认为这是一个既定的解决方案:

http://www.xs4all.nl/~peterned/csshover.html http://www.xs4all.nl/~peterned/csshover.html

With 600+ elements, a scriptless workaround is probably preferable, especially if older clients are involved. 使用600多个元素时,最好采用无脚本的解决方法,尤其是在涉及较旧的客户端的情况下。

$('textarea, input:text').focus(function() {
     $(this).addClass('hilite');
}).blur(function() {
     $(this).removeClass('hilite')
});

.hilite {
  border: 2px solid gray;
}

blur/focus will work for you. 模糊/对焦将为您工作。 If you're able, at some point, to move all your users all the way to IE8 you can also accomplish the desired effect with CSS: 如果您能够在某个时候将所有用户一直迁移到IE8,则还可以使用CSS达到预期的效果:

input[type=text]:focus { 
    background-color: lightyellow; 
}

有一个免费的小部件库可用于此任务: 焦点突出小部件

This doesn't answer your question, but is an alternative... jQuery Tools Expose will apply an overlay to all elements outside your input box thus forcing the user to focus on the input. 这不会回答您的问题,但是是替代方法。jQuery Tools Expose将对输入框外的所有元素应用覆盖,从而迫使用户将注意力集中在输入上。 It's a nice feature and the plugin is very lightweight. 这是一个很好的功能,该插件非常轻巧。 I also posted some coding that does the same thing in this answer in case you don't want to use a plugin. 如果您不想使用插件,我还会在此答案中发布一些功能相同的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM