onBlur 和 onFocus 位于单独的 jQuery 中

发布于 2025-01-02 16:57:04 字数 352 浏览 6 评论 0原文

这个 jQuery 不起作用。我知道这看起来有点奇怪。但我怎样才能让它发挥作用呢?

JavaScript 部分中的 CSS 修改不起作用。在 .blur 上,如果字段中没有任何内容,则 jQuery 应更改文本框的颜色。

模糊上,如果字段中没有任何内容,则文本应该更改并且文本颜色应该更改。在焦点上,如果字段中存在“搜索范围”,则文本应更改且文本颜色应更改。

http://jsfiddle.net/7P5J8/

我必须删除内联 JavaScript 吗?

This jQuery isn't working. I know it looks kind of odd. But how can I get it to work?

The CSS modifications in the JavaScript section are not functioning. On .blur if the field has nothing in it, the jQuery should change the color of the text box.

On the blur if there is nothing in the field, the text should change and the text color should change. On the focus if there is "Search Within" in the field, the text should change and the text color should change.

http://jsfiddle.net/7P5J8/

Do I have to remove the inline JavaScript?

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

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

发布评论

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

评论(4

巴黎盛开的樱花 2025-01-09 16:57:04

尝试一下,看看是否可以解决问题:

$("#additionalsearch").focus(function(){
if (this.value == "Search Within")
{
    this.value = "";
    $(this).css('color','#000000');
 }
});

$("#additionalsearch").blur(function(){

        if (this.value == "")
        {
            this.value = "Search Within";
            $(this).css('color','#a9a9a9');
        }
    });

http://jsfiddle.net/GhA2u/

try this and see if it fixes the problem :

$("#additionalsearch").focus(function(){
if (this.value == "Search Within")
{
    this.value = "";
    $(this).css('color','#000000');
 }
});

$("#additionalsearch").blur(function(){

        if (this.value == "")
        {
            this.value = "Search Within";
            $(this).css('color','#a9a9a9');
        }
    });

http://jsfiddle.net/GhA2u/

征棹 2025-01-09 16:57:04

您必须删除内联代码,因为它在事件处理程序之前运行。
您应该保留 HTML 仅用于表示,所有逻辑和 JavaScript 都放在

修复了 JSFiddle

JQuery

$(document).ready(function() {
    $('input#additionalsearch').focus(function() {
        if (this.value === 'Search Within') {
            $(this).css('color', '#000000').val('');
        }
    });
    $('input#additionalsearch').blur(function() {
        if (this.value === '') {
            $(this).css('color', 'red').val('Search Within');
        }
    });
});

HTML

<input id="additionalsearch" type="text" value="Search Within" />

you will have to remove the inline code, because it runs before the event handlers.
You should keep the HTML for persenation only, All the logic and JavaScript put inside the <script>tag and in a .JS file is even better.

fixed JSFiddle

JQuery

$(document).ready(function() {
    $('input#additionalsearch').focus(function() {
        if (this.value === 'Search Within') {
            $(this).css('color', '#000000').val('');
        }
    });
    $('input#additionalsearch').blur(function() {
        if (this.value === '') {
            $(this).css('color', 'red').val('Search Within');
        }
    });
});

HTML

<input id="additionalsearch" type="text" value="Search Within" />
噩梦成真你也成魔 2025-01-09 16:57:04

通过 Firebug 运行代码并查看 css 属性/属性是什么。内联应该覆盖 javascript 属性设置/css 样式属性,除非您使用重要!属性。

Run the code through Firebug and see what the css properties/attributes are. Inline should override javascript attribute settings/css styling attributes, unless you use the Important! attribute.

情话难免假 2025-01-09 16:57:04

我最好的猜测是内联函数首先运行,因此当该字段为空并且您单击时,它会被设置为具有内联函数的内容。因此,无论如何,当调用 jquery onblur 时,该字段中始终包含文本(因为即使没有文本,并且调用 onblur 时,内联函数也会在 jquery 运行之前添加文本)。

您能详细说明一下您希望发生什么吗?也许我可以帮忙。

My best guess is that the inline functions are running first, so when the field is blank and you click out, it is set to something with the inline function. So not matter what, when the jquery onblur is called, the field always has text in it (because even when it doesn't and the onblur is called the inline function adds text before the jquery runs).

Could you elaborate on what you want to occur? Maybe I can help.

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