bootstrap,JQuery加载函数;无法从外部html文档加载元素

bootstrap,JQuery加载函数;无法从外部html文档加载元素,jquery,twitter-bootstrap,load,external,Jquery,Twitter Bootstrap,Load,External,我拼命地学习一个基本教程,介绍如何使用JQuery从外部页面加载html元素 我有一个名为oltest.html的html文档,它只包含: <ol id="projects"> <li>un</li> <li>deux</li> <li>trois</li> </ol> 以及加载ol元素的第一个html文档旁边的html文档test.html: <!DOCTYPE html> <h

我拼命地学习一个基本教程,介绍如何使用JQuery从外部页面加载html元素

我有一个名为oltest.html的html文档,它只包含:

<ol id="projects">
<li>un</li>
<li>deux</li>
<li>trois</li>
</ol>
以及加载ol元素的第一个html文档旁边的html文档test.html:

<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="Test">
    <meta name="author" content="">
    <link rel="icon" href="favicon.ico">

    <title>Test</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="home.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <!--script src="../../assets/js/ie-emulation-modes-warning.js"></script-->

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>
    <div>   
    <!--ol id="projects">
    <li>un</li>
    <li>deux</li>
    <li>trois</li>
    </ol-->


    <ol id="new-projects"></ol>

    </div> <!-- /container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script>
        $( "#new-projects" ).load( "oltest.html #projects li" );
    </script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <!--script src="../../assets/js/ie10-viewport-bug-workaround.js"></script-->
  </body>
</html>

但这不起作用。有人知道吗?提前谢谢。

这是一个有趣的问题。它可能没有加载外部HTML文件,因为您正在本地运行测试。当我在本地运行代码时,出现以下控制台错误:

无法加载XMLHttpRequestfile:///path/to/your/html/file. 跨源请求仅支持协议方案:http、数据、chrome、chrome扩展、https、chrome扩展资源

您可能需要查看此stackoverflow线程:


希望这有帮助

谢谢,我现在明白了。我发现了一些技巧: