asp.net JQuery .hover 事件给出“预期对象” 错误
这就是我
在我的 .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
我使用的 $(this).hover 应该是 $('.ui-button').hover 我最初使用的,但我将其输入为 < strong>$(".ui-button").hover 注意双引号!
在我的 .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
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!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将您的代码更改为此...
我不相信“this”具有您使用这行代码时所定位的上下文...
尝试将上面的行替换为以下行...
Try changing your code to this....
I do not believe that "this" has the context that you were targeting when you use this line of code...
Try replacing the line above with the following...
这会给你更多你想要的东西吗?
另外,请确保您在上面的某个位置包含了 jquery 库。
would this give you more of what you want?
also, make sure you're including the jquery library somewhere above this.
在当前的代码片段中,$(this) 指的是$(document)。 试试这个:
现在 $(this) 引用 $('.ui-button'),这就是你想要的。
In your current code snippet, $(this) is referring to $(document). Try this:
Now $(this) is referring to $('.ui-button'), which is what you want.