如何自动滚动到文本文件窗口的底部? [英] How to automatically scrolling to the bottom of a text file window?
问题描述
我有一个按钮,可以在新窗口中打开 TXT 文件.有没有办法使用 javascript 或 php 自动转到该页面的最底部?或者到任何特定位置(例如搜索字符串)?因为是TXT文件,所以没有锚点.
I have a button that opens a TXT file in a new window. Is there a way to automatically go to the very bottom of that page using javascript or php? Or to any particular location (like searching for a string)? Because it is a TXT file, there are no anchors.
这是我的按钮的点击:
onclick="window.open('comments.txt','_comments').focus();"
我已经考虑将其添加到 onclick(但它不起作用):
I have looked into adding this to the onclick (but it did not work):
w.scrollTo(0,150);
推荐答案
其实很简单:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>This is a test</title>
</head>
<body>
<button id="open">Open text file</button>
<script>
document.getElementById('open').onclick = function(){
window.open('comments.txt','_comments').onload = function(){
this.scrollTo(0, 99999); // Use the biggest value you can
};
};
</script>
</body>
</html>
确保您从服务器(不是本地)执行此操作,因为浏览器会检查文件是否位于同一域中(出于安全原因).如果你想直接在你的机器上工作,安装一个本地服务器并使用http://localhost/
.
注意:在这里,我滚动到 99999px,因为没有实际的 HTML 文档,我们无法找出文档高度.如果这还不够,请使用更高的值.
Note: Here, I scroll to 99999px, because without an actual HTML document, we're not able to find out the document height. If that's not enough, use a higher value.
这篇关于如何自动滚动到文本文件窗口的底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!