您好,我有一个由 JavaScript 生成的 html。我还有另一个单独的 JavaScript,可以隐藏该 html 并对其执行其他操作。问题是 html 没有响应任何其他 javascript 的函数或方法。
我尝试在该 Javascipt 中包含 Javascript,但这似乎不起作用
HTML
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>
<script src = "js/java1.js"type="text/javascript"></script>
<script src = "js/java2.js"type="text/javascript"></script>
</head>
<body id = "example">
</body>
Javascript1
function creatething(){
var html ='';
html += '<div id = "button">Button <div id = "hide"> EXAMPLE </div> </div>'
return html;
}
function insertHTML(id, html) {
var el = document.getElementById(id);
if(!el) {
alert('Element with id ' + id + ' not found.');
}
el.innerHTML = html;
}
function run() {
var html = creatething();
insertHTML('example', html);
}
window.onload = run;
Javascript2
$(document).ready(function() {
$('#hide').hide();
});
最佳答案
您遇到的问题是事件的顺序。
window.onload = run; <-- Creates button
$(document).ready(function() { <-- hides button
文档就绪在加载之前触发。您添加元素 onload 并隐藏它 onready。
这就像在做披萨之前吃披萨。
关于javascript - HTML 生成的 JavaScript 不能与其他 JavaScript 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26021887/