asp.net JQuery .hover 事件给出“预期对象” 错误

发布于 2024-07-27 05:44:33 字数 2250 浏览 4 评论 0原文

这就是我

在我的 .master 页面上的内容,我有以下内容

<script type="text/javascript">
$(document).ready(function() {
    $(this).hover(function() {
        $(this).addClass("ui-state-hover");
    },
        function() {
            $(this).removeClass("ui-state-hover");
        }
    ).mousedown(function() {
        $(this).addClass("ui-state-active");
    })
    .mouseup(function() {
        $(this).removeClass("ui-state-active");
    });
});

,我的 asp.net 按钮声明如下

<asp:button runat="server" id="cmdSubmitShipmentRequest" cssclass="ui-button ui-state-default ui-corner-all" text="Submit" />

预期的行为是,每当鼠标指针悬停在任何按钮内外时,都会添加和删除上面声明的 css 类。 目前,默认的 css 属性正确应用,但每当我将鼠标悬停在按钮上或移出按钮时,我都会收到以下错误“Object Expected”第 11 行(IE8 其他浏览器不显示任何内容),这是 "

关于我可能做错了什么的任何线索吗?

编辑答案


I was doing two things wrong

  1. 我使用的 $(this).hover 应该是 $('.ui-button').hover 我最初使用的,但我将其输入为 < strong>$(".ui-button").hover 注意双引号!

  2. 在我的 .master 页面上,我使用 asp.net javascript 管理器声明我的 javascript,如下所示

问题是我的 javascript 代码块下面有脚本声明!

最后,这就是 javascript 和代码块的样子,并且一切都按预期运行良好。

<asp:scriptmanager id="scriptManager" runat="server">
    <scripts>
        <asp:scriptreference path="~/javascript/jquery-1.3.2.min.js" />
        <asp:scriptreference path="~/javascript/jquery-ui-1.7.2.custom.min.js" />
        <asp:scriptreference path="~/Javascript/thickbox.js" />
    </scripts>
</asp:scriptmanager>

 <script type="text/javascript">
     $(document).ready(function() {
         $('.ui-button').hover(function() {
             $(this).addClass("ui-state-hover");
         },
            function() {
                $(this).removeClass("ui-state-hover");
            }
        ).mousedown(function() {
            $(this).addClass("ui-state-active");
        })
        .mouseup(function() {
            $(this).removeClass("ui-state-active");
        });
     });
</script> 

This is what I have

On my .master page i have the following

<script type="text/javascript">
$(document).ready(function() {
    $(this).hover(function() {
        $(this).addClass("ui-state-hover");
    },
        function() {
            $(this).removeClass("ui-state-hover");
        }
    ).mousedown(function() {
        $(this).addClass("ui-state-active");
    })
    .mouseup(function() {
        $(this).removeClass("ui-state-active");
    });
});

and my asp.net buttons are declared as follows

<asp:button runat="server" id="cmdSubmitShipmentRequest" cssclass="ui-button ui-state-default ui-corner-all" text="Submit" />

The expected behavior is that any time the mouse pointer hovers in and out of any button the above declared css classes are added and removed. Currently the default css properties apply properly but any time I hover in and out of a button I get the following error "Object Expected" Line 11 (IE8 other browsers display nothing), which is the position where the "<script type="text/javascript">" begins.

any clue as to what I may be doing wrong?

EDIT WITH ANSWER



I was doing two things wrong

  1. I was using $(this).hover should of been $('.ui-button').hover which I had initially but I typed it as $(".ui-button").hover NOTICE THE DOUBLE QUOTES!

  2. On my .master page I am declaring my javascript using the asp.net javascript manager as follows

the problem was that I had the script declarations below my javascript code block!.

In the end this is what the javascript and code block look like and all works well as expected.

<asp:scriptmanager id="scriptManager" runat="server">
    <scripts>
        <asp:scriptreference path="~/javascript/jquery-1.3.2.min.js" />
        <asp:scriptreference path="~/javascript/jquery-ui-1.7.2.custom.min.js" />
        <asp:scriptreference path="~/Javascript/thickbox.js" />
    </scripts>
</asp:scriptmanager>

 <script type="text/javascript">
     $(document).ready(function() {
         $('.ui-button').hover(function() {
             $(this).addClass("ui-state-hover");
         },
            function() {
                $(this).removeClass("ui-state-hover");
            }
        ).mousedown(function() {
            $(this).addClass("ui-state-active");
        })
        .mouseup(function() {
            $(this).removeClass("ui-state-active");
        });
     });
</script> 

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

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

发布评论

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

评论(3

飘然心甜 2024-08-03 05:44:33

尝试将您的代码更改为此...

$(document).ready(function() {
    //SELECT THE .ui-button CLASS INSTEAD OF this
    $('.ui-button').hover(function() {
        $(this).addClass("ui-state-hover");
    },
                function() {
                    $(this).removeClass("ui-state-hover");
                }
        ).mousedown(function() {
            $(this).addClass("ui-state-active");
        })
        .mouseup(function() {
            $(this).removeClass("ui-state-active");
        });
});

我不相信“this”具有您使用这行代码时所定位的上下文...

$(this).hover(function() {

尝试将上面的行替换为以下行...

$('.ui-button').hover(function() {

Try changing your code to this....

$(document).ready(function() {
    //SELECT THE .ui-button CLASS INSTEAD OF this
    $('.ui-button').hover(function() {
        $(this).addClass("ui-state-hover");
    },
                function() {
                    $(this).removeClass("ui-state-hover");
                }
        ).mousedown(function() {
            $(this).addClass("ui-state-active");
        })
        .mouseup(function() {
            $(this).removeClass("ui-state-active");
        });
});

I do not believe that "this" has the context that you were targeting when you use this line of code...

$(this).hover(function() {

Try replacing the line above with the following...

$('.ui-button').hover(function() {
桜花祭 2024-08-03 05:44:33

这会给你更多你想要的东西吗?

另外,请确保您在上面的某个位置包含了 jquery 库。

$(document).ready(function() {
    $('.ui-button').hover(function() {
        $(this).addClass("ui-state-hover");
    },
                function() {
                    $(this).removeClass("ui-state-hover");
                }
        ).mousedown(function() {
            $(this).addClass("ui-state-active");
        })
        .mouseup(function() {
            $(this).removeClass("ui-state-active");
        });
});

would this give you more of what you want?

also, make sure you're including the jquery library somewhere above this.

$(document).ready(function() {
    $('.ui-button').hover(function() {
        $(this).addClass("ui-state-hover");
    },
                function() {
                    $(this).removeClass("ui-state-hover");
                }
        ).mousedown(function() {
            $(this).addClass("ui-state-active");
        })
        .mouseup(function() {
            $(this).removeClass("ui-state-active");
        });
});
无需解释 2024-08-03 05:44:33

在当前的代码片段中,$(this) 指的是$(document)。 试试这个:

$(document).ready(function() {  
    $('.ui-button').hover(function() {  
        $(this).addClass("ui-state-hover");  
    }, <rest of code omitted>
}

现在 $(this) 引用 $('.ui-button'),这就是你想要的。

In your current code snippet, $(this) is referring to $(document). Try this:

$(document).ready(function() {  
    $('.ui-button').hover(function() {  
        $(this).addClass("ui-state-hover");  
    }, <rest of code omitted>
}

Now $(this) is referring to $('.ui-button'), which is what you want.

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