jquery IE无法加载到div

时间:2016-09-15 01:37:03

标签: javascript jquery html css internet-explorer

我的index.html页面有一个<div id="contentarea"> </div>,我需要从位于:content文件夹中的外部* .html文件加载数据。作为示例,我在内容文件夹中创建了两个文件:

content/aboutus.html
content/contactus.html

要加载数据,我在hashchange触发器上使用jquery。

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="scripts/loader.js"></script>
</head>
<body>
        <div class="nav">
            <ul class="navigationList">
                <a href="#aboutus"><li>about us</li></a>
                <a href="#contactus"><li>contact us</li></a>
            </ul>
        </div>
<div id="contentarea"></div>
</body>
</html>

在loader.js文件中,我得到了一个非常简单的脚本,可以将数据加载到contentarea div中。

var href='';
 $(window).on('hashchange', function(){
    href = window.location.hash.substr(1) +".html";
    $('#contentarea').load('content/'+href +" #contentarea > *");
 });

我在Firefox和Safari中成功测试了它,直到我进入Internet Explorer。 IE完全忽略了$('#contentarea').load();

我已经有一段时间了,但似乎如果没有外界的帮助,我将永远陷入困境。

为了解决这种情况,我编写了代码来检测IE。

var href = '';
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
var edge = ua.indexOf('Edge/');
var trident = ua.indexOf('Trident/');

 $(window).on('hashchange', function(){
    href = window.location.hash.substr(1) +".html";

    if (msie > 0  || edge >0 || trident > 0){
         //perform the load in IE working code should go in here
         alert( "Load was performed." );
    }
    else{
        $('#contentarea').load('content/'+href +" #contentarea > *");
    }
 });

我查看了论坛帖子,但无法弄清楚如何解决问题。 有人请帮我解决如何在Internet Explorer中将数据加载到div中的问题吗?相同的代码是否适用于所有IE版本?

0 个答案:

没有答案