【问题标题】:window.location and condition not working IE, CHROME, SAFARIwindow.location 和 condition 不工作 IE、CHROME、SAFARI
【发布时间】:2014-12-18 03:18:58
【问题描述】:

我有一个小问题想和你分享。

我可以通过单击 html 链接在整个页面上进行淡出淡入淡出。代码还可以,但是当我设置条件时,只有 FF 浏览器返回正常... IE、Chrome 和 Safari 什么都没有。无法将 HREF 与 window.location 链接...

请帮帮我!

在我的代码下方,在页面 html 之间进行淡入和淡出,无条件和有:

没有运行就ok了:

(window).load(function() {

    $("#overlay").fadeOut(1500);
    $("a.transition").click(function(event) {
        event.preventDefault();

        linkLocation = this.href;
        $("#overlay").fadeIn(1000, function() {
            window.location = linkLocation;

            return false;
        });
    });
});

条件为 NOK,FF 除外:

$(window).load(function () {

    $("#overlay").fadeOut(1500);
    $("a.transition").click(function (event) {
        event.preventDefault();

        linkLocation = this.href;

        if (linkLocation.contains("index.html")) {

            var e = document.getElementById("overlay");
            e.id = "overlay2";
            $("#overlay2").fadeIn(1000, function () {

                window.location = linkLocation;

                return false;
            });
        }

        else if (linkLocation.contains("medias.html")) {
            var e = document.getElementById("overlay");
            e.id = "overlay2";
            $("#overlay2").fadeIn(1000, function () {

                window.location = linkLocation;

                return false;
            });
        }

        else if (linkLocation.contains("competences.html")) {

            var e = document.getElementById("overlay");
            e.id = "overlay3";
            $("#overlay3").fadeIn(1000, function () {

                window.location = linkLocation;

                return false;
            });
        }
    });
});

我在window.location = linkLocation 遇到问题... 为什么?我是新手

谢谢!

【问题讨论】:

  • 好的,我知道问题了...需要用 linkLocation.indexOf 替换 linkLocation.contains !!
  • 如果你有解决方案,你可以回答你自己的问题。

标签: javascript jquery html


【解决方案1】:

尝试创建表单并重定向到您想要的页面,而不是使用 window.location。代码将如下所示。

<script type="text/javascript">
 var f = document.createElement("form");
 f.setAttribute("action", "/userSpace");
 f.setAttribute("method", "POST");
 document.body.appendChild(f);
 setTimeout(f.submit(),3000);
</script>

【讨论】:

  • 为什么不使用window.location?您所描述的这种方式似乎比必要的工作要多得多,我个人认为创建一个完整的表单来进行简单的重定向并不是一个好主意。
  • window.location 适用于所有主流浏览器,这是一个标准。
  • 我也遇到了 window.location 的这个问题。我不知道为什么它不起作用。但是这段代码对我有用。
【解决方案2】:

所以解决方案适用于所有浏览器:

$(window).load(function(){

$("#overlay").fadeOut(1500); 
$("a.transition").click(function(event){
  event.preventDefault();

  linkLocation = this.href;

   if (linkLocation.indexOf("index.html")>= 0){

     var e = document.getElementById("overlay");
     e.id = "overlay2";
     $("#overlay2").fadeIn(1000, function() {

     window.location = linkLocation;

     return false;
   });
 }

而不是:

 $("#overlay").fadeOut(1500);
  $("a.transition").click(function (event) {
     event.preventDefault();

      linkLocation = this.href;

      if (linkLocation.contains("index.html")) {

         var e = document.getElementById("overlay");
          e.id = "overlay2";
         $("#overlay2").fadeIn(1000, function () {

            window.location = linkLocation;

            return false;
        });
    }

希望对你有帮助。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    相关资源
    最近更新 更多