MasterPage 中的 ASP.NET JavaScript 不起作用
我将通用(对于我的所有内容页面)js 放在我的母版页的头部部分。
<head runat="server">
<script type="text/javascript" src="../Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="../Scripts/jquery.corner.js?v2.11"></script>
<script type="text/javascript" src="../Scripts/jquery.timers.js"></script>
<script type="text/javascript" language="javascript">
var mouseOver = false;
$('.right_menu_links').hover(
function () {
var visible = $(this).parent().children(".right_menu_hint").css("display");
mouseOver = true;
if (visible == 'none') {
$(this).oneTime("1s", function () {
if (mouseOver)
$(this).parent().children(".right_menu_hint").show("slow");
});
}
},
function () {
mouseOver = false;
$(this).parent().children(".right_menu_hint").hide("slow");
}
);
</scipt>
正如我所期望的那样,我的所有内容页面脚本都应将悬停事件附加到所有 right_menu_links。但这不起作用。
当我将相同的脚本放置在我的内容页面时,一切正常! 怎么了?
I put common(for all my content pages) js to head section at my masterpage.
<head runat="server">
<script type="text/javascript" src="../Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="../Scripts/jquery.corner.js?v2.11"></script>
<script type="text/javascript" src="../Scripts/jquery.timers.js"></script>
<script type="text/javascript" language="javascript">
var mouseOver = false;
$('.right_menu_links').hover(
function () {
var visible = $(this).parent().children(".right_menu_hint").css("display");
mouseOver = true;
if (visible == 'none') {
$(this).oneTime("1s", function () {
if (mouseOver)
$(this).parent().children(".right_menu_hint").show("slow");
});
}
},
function () {
mouseOver = false;
$(this).parent().children(".right_menu_hint").hide("slow");
}
);
</scipt>
AS I expect, at all my content pages script should attach hover event to all to all right_menu_links. But it doesn't work.
When I place the same script at my content pages all is work!
What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一旦 DOM 准备好,您的代码就会执行。 JQuery 有一个名为 Ready 的方法可以为您完成此操作。只需将您的代码更改为:
You code has execute once the DOM is ready. JQuery has a method called Ready that does this for you. Just change your code to this:
我的猜测是,你的问题就在这里
,如果母版页和内容页位于不同的位置,则无法找到 javascript。
例如,您的母版页位于 http://mysite.com/masterpages/root.master
并且页面是http://mysite.com/default.aspx,它将无法工作。
输入绝对路径(http://mysite.com/Scripts/jquery-1.4.1.js)或根相对路径(/Scripts/jquery-1.4.1.js)。
My guess is that your problem is here
If master page and content pages are in different locations then javascript can not be found.
For example your master page is in http://mysite.com/masterpages/root.master
And page is http://mysite.com/default.aspx it will not work.
Put an absolute path (http://mysite.com/Scripts/jquery-1.4.1.js ) or root relative path (/Scripts/jquery-1.4.1.js).
在每个链接处添加
language="javascript"
现在重试
add
language="javascript"
at every linknow try again
它需要包装在一个函数中,该函数在 DOM 加载 时调用。
It needs to be wrapped in a function that is called when the DOM has loaded.
它不起作用,因为当 Javascript 执行时,正文中的内容尚不可用。文档正文准备就绪后,请使用以下结构执行 Javascript。
It didn't work because when the Javascript got executed, the content in the body is not yet available. Use the following structure to have your Javascript executed once the document body is ready.