虽然 javascript 解决方案是可能的,但不鼓励。PHP 旨在执行诸如随机更改站点之类的操作。假设您知道这一点,我将跳转到 javascript 解决方案。
因为您想将单词变体存储在文本文件中,您需要使用 AJAX 下载此文件或.js
使用数组或字符串将其存储在文件中。
然后你会想要改变的话。使用 AJAX 可以在页面加载时更改单词(因此它们可以但不必在查看者眼前更改)。
更改页面 HTML
可能的更改方式(单词在数组中):
wordlist.js
var status = "IN"; //Edit IN to OUT whenever you want
索引.html
<script src="wordlist.js"></script>
<div>Doctor is <span id="changing">IN</span></div>
<script>
function changeWord(s) { //Change to anything
document.getElementById("changing").innerHTML = s;
}
changeWord(status); //Get the status defined in wordlist.js
</script>
从服务器重新加载
如果您想动态更改答案并在所有打开的页面上显示更改效果,则需要 AJAX 或者您必须让浏览器重新加载单词列表,如下所示:
重新加载脚本
function reloadWords() {
var script = document.createElement("script"); //Create <script>
script.type="text/javascript";
script.src = "wordlist.js"; //Set the path
script.onload = function() {changeWord(status)}; //Change answer after loading
document.getElementsByTagName("head")[0].appendChild(script); //Append to <head> so it loads as script. Can be appended anywhere, but I like to use <head>
}
使用 AJAX
这里我们假设使用文本文件。我猜最简单的解决方案。使用 AJAX,它看起来很像这样:
http = ActiveXObject==null?(new XMLHttpRequest()):(new ActiveXObject("Microsoft.XMLHTTP"));
http.onloadend = function() {
document.getElementById("changing").innerHTML = this.responseText; //Set the new response, "IN" or "OUT"
}
http.open("GET", "words.txt")
http.send();
使用长轮询可以提高 AJAX 调用的性能。除非有人感兴趣,否则我不会在这里更多地介绍这个功能。