【问题标题】:jQuery/AJAX HTML injection and Gallery PluginsjQuery/AJAX HTML 注入和图库插件
【发布时间】:2013-06-27 09:57:31
【问题描述】:

我一直在玩一些 jQuery 并从文件中加载 HTML,但遇到了麻烦。基本上,我希望页面提取一个URL参数,加载一个与data.html对应的文件,其中数据是gallery的参数值,然后将存储在data.html中的HTML注入到divslider中,然后让 NivoSlider(一个 Jquery 库)连接到该库。

一切正常 - 找到参数,加载文件,更改 HTML(根据 firebug)。但画廊只是坐在那里闲置,好像它认为没有 HTML 被更改。

这是我的 Jquery:

//With respect to http://www.jquery4u.com/snippets/url-parameters-jquery/
    $.urlParam = function(name){
        var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
            if (!results) { return 0; }
            return results[1] || 0;     
        }
    $(window).load(function() {
        //Attempt to extract the parameter for gallery.
        var album = $.urlParam('gallery');
        var path = album+ ".html";
        //Is this legit?
        if(album != 0){
            //Kick into loading the gallery.
            $("#slider").load(path);
            //Load the gallery?
            $('#slider').nivoSlider({
                effect: 'fade',
                animSpeed:0,
                directionNav: true
            });
        }else{
            path = "a.html"; //Reset path.
            $("#slider").load(path); //Load.
            //Load gallery
            $('#slider').nivoSlider({
                effect: 'fade',
                animSpeed:0,
                directionNav: true
            });

        }       
    });

HTML 文件很简单:

<img src="a/img1.jpg" alt=""/>
<img src="a/img2.jpg" alt=""/>

我需要刷新页面吗?我在 localhost Web 服务器中执行所有这些操作,所以我知道 AJAX 请求工作正常,并且 firebug 确认 HTML 已正确注入。

有什么想法吗?提前致谢!

【问题讨论】:

  • 并且您确定您的图像路径就该脚本在服务器上的位置以及这些图像的位置而言是正确的??
  • 可能是 animSpeed:0 阻止了过渡的发生。如果你把它设置为 500,你还有这个问题吗?
  • @redneckjedi 更改了 animSpeed,但这并没有解决问题。
  • @kylek 是的,萤火虫证明 html 是从文件中正确注入的

标签: jquery html ajax


【解决方案1】:

也许您必须等到 ajax 请求完成后才能设置滑块。

    $("#slider").load(path, function(){
        //Load the gallery?
        $('#slider').nivoSlider({
            effect: 'fade',
            animSpeed:0,
            directionNav: true
        });
     });

【讨论】:

  • 啊!你真是个天才!非常感谢,立即修复!我曾经用 Ajax 做过两件事,所以我仍然不喜欢它。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
相关资源
最近更新 更多