我在本地系统中存储了一个 html 页面,我尝试获取内容然后写入 iframe。
<html>
<head>
<title></title>
<script src="js/jquery.min.js" type="text/javascript"></script>
<link href="styles/myStlye.css" rel="stylesheet" type="text/css" />
</head>
<body><button id="buttonClick">Click Me</button></body>
<script>
$(document).ready(function(){
$("#buttonClick").click(function(){
alert("Button clicked.");
});
});
</script>
</html>
我使用 document.write() 将 html 代码写入 iframe,但在 IE9 中加载页面时出现错误 SCRIPT5009: '$' is undefined,但在 google chrome 和 firefox 中工作正常.
最佳答案
问题可能是 IE 在加载 jquery 之前执行了 JS 代码,尝试动态加载并监听 onload:
<html>
<head>
<title></title>
<link href="styles/myStlye.css" rel="stylesheet" type="text/css" />
</head>
<body>
<button id="buttonClick">Click Me</button>
<script>
var script = document.createElement("script");
script.src = "js/jquery.min.js";
script.onreadystatechange = function () {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
$("#buttonClick").click(function () {
alert("Button clicked.");
});
}
};
script.onload = function () {
$("#buttonClick").click(function () {
alert("Button clicked.");
});
};
document.querySelector('body').appendChild(script);
</script>
</body>
</html>
关于javascript - 使用 document.write() 将 html 写入 iframe,但 jquery 在 IE9 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53019445/