javascript - 打开 .txt 文件并在 JavaScript 中滚动到其底部

标签 javascript html

我的问题与以下问题相同,只是我想在同一选项卡/窗口而不是新选项卡/窗口中打开 .txt 文件:

How to automatically scroll to the bottom of a text file window?

可以the solution以某种方式修改后仍然可以工作,仅在同一个选项卡/窗口中?

我尝试将 _comments 更改为 _self 但随后滚动停止发生。

最佳答案

您提到的答案使用Window.open() 。来自 MDN:

The Window interface's open() method loads the specified resource into the browsing context (window or tab) with the specified name. If the name doesn't exist, then a new window is opened and the specified resource is loaded into its browsing context.

Syntax

var window = window.open(url, windowName, [windowFeatures]);

Parameters

url

A DOMString indicating the URL of the resource to be loaded. This can be a path or URL to an HTML page, image file, or any other resource which is supported by the browser. If the empty string ("") is specified as url, a blank page is opened into the targeted browsing context.

windowName

A DOMString specifying the name of the browsing context (window or tab) into which to load the specified resource; if the name doesn't indicate an existing context, a new window is created and is given the name specified by windowName. This name can then be used as the target of links and forms by specifying it as the target attribute of or elements. The name should not contain whitespace. Keep in mind that this will not be used as the window's displayed title.

_comments只是一个随机名称,不应使用它,以便在新窗口中打开它。你不能使用该功能。要在同一窗口/选项卡中打开,您需要使用其他内容,例如 Window.location

The Window.location read-only property returns a Location object with information about the current location of the document.

Though Window.location is a read-only Location object, you can also assign a DOMString to it. This means that you can work with location as if it were a string in most cases:

location = 'http://www.example.com'

is a synonym of

location.href = 'http://www.example.com'

Example #1: Navigate to a new page

Whenever a new value is assigned to the location object, a document will be loaded using the URL as if location.assign() had been called with the modified URL. Note that security settings, like CORS, may prevent this to effectively happen.

location.assign("http://www.mozilla.org"); // or
location = "http://www.mozilla.org";

但是,一旦加载 .txt 文件,您的 JavaScript 就会消失。因此,在浏览器中加载纯 .txt 文件可能不是最好的主意。如果您使用 jQuery,请考虑创建 <div id="textfile"> 。您可以通过执行以下操作将文本文件内容放入其中:

$("#textfile").load("file.txt", function() {
    // do your scrolling here
});

就滚动而言,在普通的 JS 中这是很棘手的。如果您使用 jQuery,则有 jquery.scrollTo 。然后,您将能够在您的函数中使用以下任意内容:

$.scrollTo("250px");
$.scrollTo("50%");
$.scrollTo("+=25px");
$.scrollTo("max");

关于javascript - 打开 .txt 文件并在 JavaScript 中滚动到其底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47083507/

相关文章:

php - 将用户 Gmail 联系人导入到我的网站并向他们的所有联系人发送邀请

javascript - 如何复制表单数据?

javascript - 从 React Hook 复制一个数组

javascript - 使用 knockout foreach 每 2 条记录创建新行

python - 如何使用flask将内容加载到div元素中

javascript - 无法使此 reduce 功能正常工作

javascript - 在 iOS 移动浏览器中请求位置权限弹出窗口

php - 在 HTML5 中拖放多个文件?

java - 如何使用Java代码和JSP从浏览器播放MP3?

php - 在php中使用bootstrap发生错误时如何显示错误