【问题标题】:Javascript Functions Only Run After Refresh (f5)Javascript 函数仅在刷新后运行 (f5)
【发布时间】:2014-04-19 17:37:21
【问题描述】:

背景:我为我目前正在开发的一个婚纱摄影网站购买了一个模板。我有一个关于 html 和 css 的平均知识库。从来没有真正做过 javascript,因为我大部分时间都花在编写 c# 上。您可以在

上查看该网站

http://www.nm-photography.co.uk

可以在此处找到该网站的完整工作版本:http://themeforest.net/item/jphotolio-responsive-wedding-photography-template/full_screen_preview/3057579

错误:当您打开网站时,它可以正常工作并通过 javascript (jQuery(document)) 提取所有正确的图像。但是,如果您转到另一个页面,则在您按 ctrl r / f5 之前似乎没有加载任何 javascript。如果您返回主页,则除非您按刷新,否则这也不起作用。

我的研究:所以​​我花了几个小时在 Google 上进行挖掘,是的,甚至是第 2 页!有些人使用 on.load 函数,有些人使用一些后台工作函数?同样,我对 c# 有信心,而不是 javascript!我将粘贴下面的代码,前提是它不会太长!我不太确定在哪里放置正确的代码,所以如果有人能在正确的方向上帮助我,我将不胜感激:) 谢谢!

我还注意到,在 java 脚本中,右键单击已被禁用,但是,一旦刷新页面以加载图像,您就可以右键单击该页面。

<script type="text/javascript" src="js/jegbg.js"></script>
<script type="text/javascript">

jQuery(document).ready(function($)
{   

    resize_window("#jegbgcontainer");       

    /** bind jeg default **/
    $(window).jegdefault({
        curtain : 1,
        rightclick  : 0,
        clickmsg    : "Disable Right Mouse Click"
    });

    var content = [         
        { "index":0,
          "type":"image",
          "source":"images\/emr-1-3.jpg",
          "pos":"top",
          "title":"Image Slider",
          "link":"masonry.html#!/gordon-jennas-wedding",
          "desc":"<p><strong><\/strong>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#8217;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.<\/p>\n<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<\/p>\n"},          
        {"index":1,
          "type":"image",
          "source":"images\/misc-1-3.jpg",
          "pos":"center",
          "title":"Slider to contact",
          "link":"contact.html",
          "desc":"<p><strong><\/strong>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#8217;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.<\/p>\n<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<\/p>\n"},          
        {"index":2,
          "type":"image",
          "source":"images\/464713958_1d6a51c6c0_b1.jpg",
          "pos":"top",
          "title":"Slider to Password Page",
          "link":"masonry.html#!/flower-peda",
          "desc":"<p><strong><\/strong>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#8217;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.<\/p>\n<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<\/p>\n"},                                  
        {"index":3,
          "type":"video",
          "source":{ "videotype":"youtube",
                     "src":"http:\/\/www.youtube.com\/watch?v=w3jTxLs7Bek"},
          "title":"Slider with Video",
          "link":"masonry.html#!/just-married",
          "desc":"<p><strong><\/strong>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#8217;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.<\/p>\n<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<\/p>\n"}                       
    ];

    var holddesc = undefined;

    var jegbg = $("#jegbgcontainer").jegbg({
        fade_speed                  : 700,
        delay                       : 10000,
        content                     : content,
        autostart                   : true,
        partial_load                : true
    },  function(ele, media){
        $('#homepita').fadeIn(1000);                
        $('#homepita .torapper').html(ele.title);       
        $(".homelink a").attr("href" , ele.link);
        holddesc = ele.desc;

        if(!$(".homeinfo").is(":visible")) {
            pitaSlideUp();
        }
    });

    /* binding touchwipe, disable this feature if using iphone */
    if(scw(iphonewidth)) {
        $(".texthome").touchwipe({
            wipeLeft: function(e) {                 
                jegbg.next();
                return false;
            },
            wipeRight: function() {                 
                jegbg.prev();
                return false;
            },
            min_move_x: 20,
            min_move_y: 20,
            preventDefaultEvents: true
        }); 
    }

    var pitaSlideUp = function(){
        jegbg.restart();
        $(".homeblock").slideUp("fast", function(){
            $("#homepita").animate({
                "right" : -288
            }, function(){
                $(".homeinfo").fadeIn("fast", function(){
                    $(this).attr("style","").addClass("displayblock");
                });
                $(".homeblock").removeClass("homedesc");
                $(".homedescdetail").remove();
            });
        });
    };

    $(window).resize(function(){pitaSlideUp();});

    var pitaSlideDown = function() {
        jegbg.pause();
        $("#homepita").addClass("hptoogle");
        $(".homeblockinside").prepend("<div class='homedescdetail'>" + holddesc + "</div>");
        $(".homeblock").slideDown("fast", function(){
            $(this).addClass("homedesc");
            $(".homedescdetail").css({
                height  : $('.homeblockinside').height() - 30,
                width   : $('.homeblockinside').width() - 12
            });
            jpanel = $(".homedescdetail").jScrollPane().data().jsp;             
        });
    };

    $(".homeinfo").click(function(){
        $(this).fadeOut("fast");
        $("#homepita").animate({
            "right" : -5
        }, function(){
            pitaSlideDown();
        });
    });

    $(".homeblock .closeme, .torapper").click(function(){
        pitaSlideUp();
    });
});

【问题讨论】:

  • 尽量不要把 'JavaScript' 称为 'Java',这会让人困惑,让 JS 人愤怒
  • 非常抱歉!我不知道有什么区别:/
  • 你确定你正确地包含了 jquery,因为我的 chrome 开发者工具显示 Uncaught ReferenceError: jQuery is not defined 并且没有图片来。
  • 尝试使用谷歌CDN jquery &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"&gt;&lt;/script&gt;
  • @Markus403 我试过了,但没用:/图像根本无法加载到网页上!

标签: javascript jquery html css refresh


【解决方案1】:

发现问题了!正在加载 html 文件,但有 phtml 文件。这些没有与 html 同步,因此出现了问题!感谢大家的帮助和疑难解答!

【讨论】:

    【解决方案2】:

    使用id="jegbgcontainer" 将您的脚本内容移出 div,因为该容器的内容已被替换,从而在加载另一个页面时替换您的脚本。

    【讨论】:

    • 我不明白。关闭jegbgcontainer的div标签后,脚本内容放在html页面上?
    【解决方案3】:

    很好用

    $(function(){
    
        //executes after everything is loaded 
    
    });
    

    看看有没有帮助

    还要确保您的 jQuery 引用是正确的,如果您从另一个页面导航,jQUery 链接相对路径可能会出错,但如果您刷新同一页面,则工作正常

    【讨论】:

    • 试过了,但这让网站变得更糟!在这里真的觉得我的深度不够哈哈!
    • 嗯,这实际上是加载时正确和最常见的 jQuery 方法之一,所以如果这弄乱了您的网站,那么您的代码中的某些内容是错误的......我会看看家。现在就在电话上:p
    • 好的,谢谢!我付钱给别人来解决这个问题没有问题,我不想想象这些天网络开发人员会收取多少费用!我刚刚收到更多的婚礼预订,我真的需要一个快速建立的网站!这是我见过的最好的主题并且毫不犹豫地购买了!
    • 没关系,伙计,无需解释,帮助有需要的开发人员是我们来这里的目的:)
    • 您是否尝试在代码中的每个函数后面加上alert("test")console.write 以查看代码失败的地方?还将您的jQuery(document).ready(function($) 减少到$(function(){,如果它不起作用,您可能需要下载最新的 jQuery 库并使用它们而不是您的,请在 `jquery.com `
    猜你喜欢
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2017-01-03
    • 1970-01-01
    相关资源
    最近更新 更多