我无法使用常规脚本或查询加载2个文本文件,我需要帮助,PLZ给出并回答?
继承人jquery,
<script type="text/javascript">
setTimeout(function(){
window.location.reload(1);
}, 1000);
</script>
<script language="javascript" src="file:///Users/trevor/Desktop/jquery.js" ></script>
<script type="text/javascript">
jQuery.get('file:///Users/trevor/Desktop/x.txt', function(data) {
document.getElementById('xp').innerHTML = data;
});
</script>
<script type="text/javascript">
jQuery.get('file:///Users/trevor/Desktop/y.txt', function(data) {
document.getElementById('xp').innerHTML = data;
});
</script>
<p id="xp">hi</p>
<br>
<p id="yp">hi</p>
没有,
<script type="text/javascript">
var client = new XMLHttpRequest();
client.open('GET', 'file:///Users/trevor/Desktop/x.txt');
client.onreadystatechange = function() {
document.getElementById('xp').innerHTML = client.responseText;
}
client.send();
</script>
<script type="text/javascript">
var client = new XMLHttpRequest();
client.open('GET', 'file:///Users/trevor/Desktop/y.txt');
client.onreadystatechange = function() {
document.getElementById('yp').innerHTML = client.responseText;
}
client.send();
</script>
可能......
<script type="text/javascript">
var x = document.getElementById('x').contentWindow.getElementById('xp').innerHTML
var y = document.getElementById('y').contentWindow.getElementById('yp').innerHTML
function updatex(){document.getElementById(xp).innerHTML = x}
function updatey(){document.getElementById(yp).innerHTML = y}
setInterval( "updatex(); document.getElementById('x').contentWindow.location.reload();", 1000 );
setInterval( "updatey(); document.getElementById('y').contentWindow.location.reload();", 1000 );
</script>
<p id="xp">hi</p>
<br>
<p id="yp">hi</p>
<iframe id="x" src="file:///Users/trevor/Desktop/x.txt"></iframe>
<iframe id="y" src="file:///Users/trevor/Desktop/y.txt"></iframe>
答案 0 :(得分:1)
您可以使用setInterval
定期加载文件。这里有一点点重构:
function loadFile(url, elementId){
var client = new XMLHttpRequest();
client.open('GET', url);
client.onreadystatechange = function() {
document.getElementById(elementId).innerHTML = client.responseText;
}
client.send();
}
现在你可以每秒拨打两次电话:
var interval = setInterval(function(){
loadFile('file:///Users/trevor/Desktop/x.txt', 'xp');
loadFile('file:///Users/trevor/Desktop/y.txt', 'yp');
},1000);
一些注意事项:
client
并取消它们。<iframe>
,并定期对其进行简单刷新。