JQuery Cycle插件在ASP.NET Masterpage中不起作用

时间:2012-02-16 20:27:11

标签: jquery asp.net jquery-ui jquery-cycle

我有一个使用Masterpages的.NET网站。我正在尝试在我的主页面上的菜单中添加一些功能,允许用户将鼠标悬停在一些动态链接上,这些动态链接在代码后面构建,并具有另一个动态内容区域更新,以根据用户已经过去的内容显示内容。 / p>

为了实现这一点,我试图让JQuery图像旋转器(循环插件http://jquery.malsup.com/cycle/)起作用。我也尝试过用JQueryUI做类似的事情。

我的问题是,当这个代码在Masterpage上时它永远不会起作用,但如果我把它放在我网站的任何其他页面上就没关系了。我没有使用标题中脚本文件的相对链接。

任何人都可以帮我弄清楚为什么我不能让JQuery在Masterpage上运行?

我的JQuery Cycle标题内容如下。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="http://malsup.github.com/jquery.cycle.all.js"></script>

<script type="text/javascript">
$(document).ready(function () {
    $('#slideshow').cycle({
        fx: 'turnDown',
        speed: 'fast',
        timeout: 0,
        pager: '#nav',
        pagerAnchorBuilder: function (idx, slide) {
            // return sel string for existing anchor
            return '#nav li:eq(' + (idx) + ') a';
        }
    });
});
</script>

1 个答案:

答案 0 :(得分:0)

我认为,问题是脚本加载的顺序。

可能是您在不存在的元素上调用某些内容,因此在初始化时尚未创建Cycle Plugin。

如果您有Ajax,可以考虑使用脚本管理器,但这不是必需的。

加载顺序如下:

1)Jquery加载在母版页上。 2)合并内容页面并触发内容标题脚本。 3)Document.ready从Master中调用 4)从内容页面调用Document.ready

如果你发出一些警告,你可以追踪你是否在正确的时间进行$('#slideshow')。cycle()调用。你需要确保#slideshow存在。

有关脚本管理器解决方案,请参阅此帖子: Proper way to use JQuery when using MasterPages in ASP.NET?

基本上:

1) Add Scriptmanager to your Master Page

2) Add a reference to the Jquery library before your Content Page document.ready()

3) Use your document.ready code above as it is, but do not include the references to the library or plugin files. Those belong on the master.

我知道这是一篇较老的帖子。但我认为这是一个常见的问题。

相关问题