Javascript 有人能给我解释一下为什么这不是';不行吗?Ajax加载&x2B;html上的脚本
ajax加载html内容后脚本不会加载,为什么这一步不起作用。以下是我得到的: MakoStart.html:(将更改的主页)Javascript 有人能给我解释一下为什么这不是';不行吗?Ajax加载&x2B;html上的脚本,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,ajax加载html内容后脚本不会加载,为什么这一步不起作用。以下是我得到的: MakoStart.html:(将更改的主页) document.getElementById(“makoislandbeach”).onclick=function(){ $('#makochange').load('makoisland.html'); }; 这是可行的,当我单击id为:makoislandbeach的图像时,内容将更改为makoisland.html中的内容 以下是makoisland
document.getElementById(“makoislandbeach”).onclick=function(){
$('#makochange').load('makoisland.html');
};
这是可行的,当我单击id为:makoislandbeach的图像时,内容将更改为makoisland.html中的内容
以下是makoisland.html中的内容:
<div class="makochoices">
<div id="rightside">
<img src="../img/makoislandspecial/waterstream.jpg" class="makoislandchoice" id="waterstream" alt="right choice">
</div>
<div id="leftside">
<img src="../img/makoislandspecial/islandside.jpg" class="makoislandchoice" id="islandside" alt="left choice">
</div>
</div>
<script>
document.getElementById("waterstream").onclick = function() {
$('#makochange').load('waterstream.html');
};
document.getElementById("islandside").onclick = function() {
$('#makochange').load('islandside.html');
};
</script>
document.getElementById(“waterstream”).onclick=function(){
$('#makochange').load('waterstream.html');
};
document.getElementById(“islandside”).onclick=function(){
$('#makochange').load('islandside.html');
};
一旦内容发生更改,当我单击已替换旧内容的2个新图像时,脚本不会执行。我制作了一个waterstream.html,它与makoisland.html相同,但图像不同,但脚本在第一次转换后无法工作。看看这个。正如它所说:
当使用不带后缀选择器表达式的URL调用.load()时,在删除脚本之前,内容将传递给.html()。这将在脚本块被丢弃之前执行它们。但是,如果使用附加到URL的选择器表达式调用.load(),则会在更新DOM之前剥离脚本,因此不会执行
也许你可以做些类似的事情。不要使用load,而是使用ajax。有点不同
<div class="makoislandintro" id="makochange">
<div class="makochoicesfirst">
<img src="../img/makoislandspecial/makobeach.jpg" class="makoislandbeach" id="makoislandbeach" alt="current">
<br>
</div>
</div>
<script>
document.getElementById("makoislandbeach").onclick = function() {
$.ajax({
url: "makoisland.html",
context: document.body,
success: function(responseText) {
$("#makoislandbeach").html(responseText);
$("#makoislandbeach").find("script").each(function(i) {
eval($(this).text());
});
}
});
};
</script>
document.getElementById(“makoislandbeach”).onclick=function(){
$.ajax({
网址:“makoisland.html”,
上下文:document.body,
成功:函数(responseText){
$(“#makoislandbeach”).html(responseText);
$(“#makoislandbeach”)。查找(“脚本”)。每个(函数(i){
eval($(this.text());
});
}
});
};
我认为makoisland.html中的脚本无法访问位于makostart.html中的dom元素。现在,内容甚至不会在第一次单击时更改:/any ideas?抱歉,我错过了ID。在成功部分使用#makochange而不是#makoislandbeach。它成功了,非常感谢你,伙计,被困了一段时间!
<div class="makoislandintro" id="makochange">
<div class="makochoicesfirst">
<img src="../img/makoislandspecial/makobeach.jpg" class="makoislandbeach" id="makoislandbeach" alt="current">
<br>
</div>
</div>
<script>
document.getElementById("makoislandbeach").onclick = function() {
$.ajax({
url: "makoisland.html",
context: document.body,
success: function(responseText) {
$("#makoislandbeach").html(responseText);
$("#makoislandbeach").find("script").each(function(i) {
eval($(this).text());
});
}
});
};
</script>