gpt4 book ai didi

php - 随着时间改变div内容

转载 作者:行者123 更新时间:2023-11-28 10:57:30 24 4
gpt4 key购买 nike

我尝试使用 ajax 加载一段时间后的 php 文件。我想做的是一种测验。我希望用户能够看到图像屏幕 3 秒钟,然后看到答案选项。我想连续这样做3或4次。例如;1)问题图像几秒钟后2)答案选择点击答案后3)第二题图片...并继续执行此订单。

我可以用下面的代码来做到这一点:

<script type="text/javascript">

var content = [
"<a href='resim1a.php'> link 1 </a>",
"<a href='resim2a.php'> link 2 </a>",
"insert html content"
];
var msgPtr = 0;
var stopAction = null;

function change() {
var newMsg = content[msgPtr];
document.getElementById('change').innerHTML = 'Message: '+msgPtr+'<p>'+newMsg;
msgPtr++;
if(msgPtr==2)
clearInterval(stopAction);
}

function startFunction() { change(); stopAction = setInterval(change, 500); }
window.onload = startFunction;
</script>

<div id="change" style="border:5px solid red;width:300px; height:200px;background-Color:yellow"> </div>

但是,当该文件被另一个文件包含时,该脚本将不起作用。我怎样才能让它发挥作用?

<script type="text/javascript">
function load(thediv, thefile){
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject ('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById(thediv).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', thefile , true);
xmlhttp.send();
}
</script>

上一页脚本如上。我将此脚本与以下代码一起使用:

<div id="anotherdiv" >
<input type="image" onclick="load('anotherdiv' , 'include.php');"src="buton1.png">
</div>

最佳答案

原因是因为您期望窗口触发 onload 事件:

window.onload = startFunction;

窗口是在ajax运行时加载的,为什么不直接调用函数呢?

startFunction();

如果您在脚本之前需要一些 DOM 元素,只需将脚本放在 DOM 元素下方,脚本将在 DOM 准备好后运行。

关于php - 随着时间改变div内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17469477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com