【问题标题】:Jquery load not working in firefoxJquery负载在Firefox中不起作用
【发布时间】:2014-01-30 09:12:53
【问题描述】:

我正在开发的网站上的脚本有问题。它在 Safari、Opera 和 Chrome 中运行良好,但在 Firefox 中无法运行。

当您单击链接时,我尝试将 div #contain 从内部链接加载到覆盖 div,然后将其隐藏。所有这些在我上面提到的浏览器中都可以正常工作,但在 Firefox 中无法正常工作,点击功能只是像往常一样打开链接(重新加载页面)。

任何想法为什么它在 Firefox 中不起作用?有什么我遗漏的吗?

$(document).ready(function(){


    var     $ov = $('.overlay'),
        $tp = $('#transparent'),
        URL = ' ',
        siteURL = "http://" + top.location.host.toString(),     
        $internal = $("a[href^='"+siteURL+"'], a[href^='/'], a[href^='./'], a[href^='../'], a[href^='#']:not('.no')"),      
        hash = window.location.hash,
        $el, $allLinks = $("a");


        $tp.hide();
        $ov.hide();


        $tp.click(function(){

            $ov.empty();
            $tp.hide();
            $ov.hide(); 

        });


        if (hash) {

            $ov.show();
            $tp.show();         
            hash = hash.substring(1);
            URL = hash + " #contain";

            $ov.load(URL);

            };

        $internal.each(function(){

            $(this).attr("href", "#" + this.pathname);

            }).click(function(){

                $tp.show();
                $ov.show();

                $el  = $(this);
                URL = $el.attr("href").substring(1);
                URL = URL + " #contain",

                $ov.load(URL);

            });
    });

【问题讨论】:

  • 我的一个部分视图有完全相同的问题。尝试在 $ov.load(URL); 之前添加警报;看看这是否有效。它对我有用。我仍在寻找它为什么这样做和/或如何解决它。

标签: javascript jquery firefox


【解决方案1】:

我认为您在点击功能中缺少 preventDefault。这告诉 jQuery/javascript 不要遵循默认操作,在您的情况下,默认操作将遵循 HREF 中链接指向的任何位置。

代替

$tp.click(function(){
    $ov.empty();
    $tp.hide();
    $ov.hide();
}); 

应该是

$tp.click(function(e){
    e.preventDefault;
    $ov.empty();
    $tp.hide();
    $ov.hide();
});

【讨论】:

  • 我认为点击功能甚至不是问题。我的猜测是,我定位内部链接的方式并没有按预期工作。在其他浏览器中,链接更新如下:www.website.com/#/link 但在 Firefox 中,它们仍然看起来像这样:www.website.com/link 所以我想那一定有问题。跨度>
【解决方案2】:

而不是 $tp.click(function(

试试

$(document).on('click','#transparent',function(event){
//do whatever you want to do
});

【讨论】:

  • 我试过了,结果和以前一样。还有其他建议吗?
【解决方案3】:

尝试改变浏览器的默认行为:

(event.preventDefault) ? event.preventDefault() : event.returnValue = false;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 2011-11-08
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多