bootstrap, JQuery 加载函数;无法从外部 html 文档加载元素
bootstrap, JQuery load function; cannot load elements from an external html document
我拼命地尝试遵循有关使用 JQuery 从外部页面加载 html 元素的基本教程。
我有一个名为 oltest.html 的 html 文档,其中仅包含:
<ol id="projects">
<li>un</li>
<li>deux</li>
<li>trois</li>
</ol>
和一个 html 文档,test.html,在第一个加载 ol 元素的旁边:
<!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 文件,因为 运行 您的测试是在本地进行的。当我在本地 运行 你的代码时,我得到了这个控制台错误:
XMLHttpRequest cannot load file:///path/to/your/html/file. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
您可能想看看这个 Whosebug 线程:
Dynamically loading content on local HTML page
希望对您有所帮助!
我拼命地尝试遵循有关使用 JQuery 从外部页面加载 html 元素的基本教程。
我有一个名为 oltest.html 的 html 文档,其中仅包含:
<ol id="projects">
<li>un</li>
<li>deux</li>
<li>trois</li>
</ol>
和一个 html 文档,test.html,在第一个加载 ol 元素的旁边:
<!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 文件,因为 运行 您的测试是在本地进行的。当我在本地 运行 你的代码时,我得到了这个控制台错误:
XMLHttpRequest cannot load file:///path/to/your/html/file. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
您可能想看看这个 Whosebug 线程:
Dynamically loading content on local HTML page
希望对您有所帮助!