javascript - 附加html代码后如何获取就绪事件
问题描述
我有以下jsfiddle
https://jsfiddle.net/3qge41b0/
$('#myP').click(function() {
GetMap();
});
function GetMap() {
var text = '<html lang="en"> <head> <link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css"> <style> .map { height: 400px; width: 100%; } </style> <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js" asynchronous=false defer=true id="oljs" onload="LoadMap"></script' + '> <title>OpenLayers example</title> </head> <body> <h2>My Map</h2> <div id="map" class="map"></div> <script type="text/javascript" defer=true> function LoadMap() { var map = new ol.Map({ target: "map", layers: [new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([37.41, 8.82]), zoom: 10}) }); console.log(map) }; $(function() { LoadMap() });</script' + '> </body></html>';
$("#myDIV").html(text);
}
$('#myP2').click(function() {
GetMap2();
});
function GetMap2() {
var text = '<html lang="en"> <head> <link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css"> <style> .map { height: 400px; width: 100%; } </style> <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js" asynchronous=false defer=true id="oljs" onload="LoadMap"></script' + '> <title>OpenLayers example</title> </head> <body> <h2>My Map</h2> <div id="map" class="map"></div> <script type="text/javascript" defer=true> function LoadMap() { var map = new ol.Map({ target: "map", layers: [new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([37.41, 8.82]), zoom: 10}) }); console.log(map) }; setTimeout( function() { LoadMap() }, 10);</script' + '> </body></html>';
$("#myDIV").html(text);
}
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h1>My Web Page</h1>
<button id="myP">Click me.</button>
<button id="myP2">Or Click me.</button>
<div id="myDIV" style="width=100%;height=100%">This is a div element.</div>
</body>
</html>
$('#myP').click(function() { GetMap(); });
function GetMap()
{
var text = '<html lang="en"> <head> <link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css"> <style> .map { height: 400px; width: 100%; } </style> <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js" asynchronous=false defer=true id="oljs" onload="LoadMap"></script'+'> <title>OpenLayers example</title> </head> <body> <h2>My Map</h2> <div id="map" class="map"></div> <script type="text/javascript" defer=true> function LoadMap() { var map = new ol.Map({ target: "map", layers: [new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([37.41, 8.82]), zoom: 10}) }); console.log(map) }; $(function() { LoadMap() });</script'+'> </body></html>';
$("#myDIV").html(text);
}
$('#myP2').click(function() { GetMap2(); });
function GetMap2()
{
var text = '<html lang="en"> <head> <link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css"> <style> .map { height: 400px; width: 100%; } </style> <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js" asynchronous=false defer=true id="oljs" onload="LoadMap"></script'+'> <title>OpenLayers example</title> </head> <body> <h2>My Map</h2> <div id="map" class="map"></div> <script type="text/javascript" defer=true> function LoadMap() { var map = new ol.Map({ target: "map", layers: [new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([37.41, 8.82]), zoom: 10}) }); console.log(map) }; setTimeout( function() { LoadMap() }, 10);</script'+'> </body></html>';
$("#myDIV").html(text);
}
正如您所看到的,它只是一个使用 html 字符串动态加载的 div。问题是,第一个按钮最终出现错误,因为 ol.js 库似乎没有加载。如果我设置超时,它会起作用(GetMap2)。此外,如果您第二次按下第一个按钮。
但我发现这是一种解决方法而不是解决方案。如果连接很慢并且加载库需要超过 100 毫秒怎么办?
加载库时是否有可能获取事件?我尝试了很多不同的附加或添加 html 字符串的方法。没有任何效果。ol.js 上的 ready 或 load 事件未正确触发。
有人知道如何解决这个问题吗?
亲切的问候尼科
解决方案
使用 jquery,您可以使用getScript来确保脚本已完全加载。
$.getScript("https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js", function(){ LoadMap(); });
推荐阅读
- shell - Ripgrep 仅排除文件夹根目录中的文件
- java - 如何使用java读取.log文件?
- mysql - 如何只更新那些可整除的 MySQL
- php - 如何使用 ffmpeg 功能按比例(纵横比)图像?
- excel - Excel:如果一个单元格包含两个单词,我将如何使用第一个单词的第一个字母和第二个单词的前两个字母?
- recursion - 深度优先搜索在返回时不会创建新对象
- c# - 无法在启动类中注入 IAntiforgery
- visual-studio-code - VSCode 定义永远存在
- sql - 如何在行之间分配计数 SQL Server
- c# - MVC 控制器动作中的 HttpClient