我有一個選擇列表,裏面填充了我的日誌文件。每秒JavaScript都會向讀取日誌文件並填充列表的服務器發送GET請求。但是在每次GET請求之後,列表將滾動回頂部。我想要做的是使請求不會影響滾動,以便我可以自由滾動列表。在列表中維護滾動位置
<select id = "list" name=servers size=38 style=width:1028px>
<script type="text/javascript">
window.onload = function() {
if (bytes === undefined) {
var bytes=0;
}
var url = "/test/log.php?q=";
function httpGet(url)
{
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, true);
xhttp.onload = function (e) {
if (xhttp.readyState === 4) {
if (xhttp.status === 200) {
var list = "";
console.log(xhttp.responseText);
obj = JSON.parse(xhttp.responseText);
for(var key in obj) {
list += obj[key];
if (sessionStorage.logfile == null) {
sessionStorage.logfile == "Log";
}
}
bytes = parseInt(list);
document.getElementById("list").innerHTML = sessionStorage.logfile + list;
sessionStorage.logfile += list;
}
};
xhttp.onerror = function (e) {
console.error(xhttp.statusText);
}
};
xhttp.send();
}
var updateInterval = 1000;
function update() {
httpGet(url + bytes);
setTimeout(update, updateInterval);
}
update();
}
</script>
刷新列表時總是獲取所選鍵,並在刷新後選擇.options [key] .selected。 –
@GovindSamrow你能更具體嗎? – Mirakurun
您正在替換整個列表,爲了保持滾動位置,您應該更新列表中元素的值。你能發佈一個典型的Ajax響應,所以我們可以看到你正在使用的格式? – Trey