javascript - 尝试使用 javascript 从文本文件读取文本,但方法 : split 出现错误

标签 javascript

这是我在我的网站中使用的代码:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://newsxpressmedia.com/files/theme/jquery.newsTicker.js"></script>

<script>
$(function() {
    var file = "http://newsxpressmedia.com/files/theme/test.txt";
    $.get(file, function (txt) {
        var lines = txt.responseText.split("\n");
        $ul = $('<ul class="newsticker" />');
        for (var i = 0, len = lines.length; i < len; i++) {
            $ul.append('<li>' + lines[i] + '</li>');
        }
        $ul.appendTo('body').newsTicker({
            row_height: 48,
            max_rows: 2,
            speed: 6000,
            direction: 'up',
            duration: 400,
            autostart: 1,
            pauseOnHover: 0
        });
    });
});
</script>

根本没有任何文字,它不起作用。

这是我的网站:

http://newsxpressmedia.com/

当我使用 Chrome 浏览我的网站时,然后执行:

在打开的控制台中按 Ctrl+Shift+J 我看到以下内容:

Consider using 'dppx' units instead of 'dpi', as in CSS 'dpi' means dots-per-CSS-inch, not dots-per-physical-inch, so does not correspond to the actual 'dpi' of a screen. In media query expression: only screen and (-webkit-min-device-pixel-ratio: 2), not all, not all, not all, only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx) (index):1
Uncaught TypeError: Cannot call method 'split' of undefined (index):101
event.returnValue is deprecated. Please use the standard event.preventDefault() instead.

如何修复此“拆分”错误?

** 在我的网站中向上滚动并显示的文本是其他有效的脚本/代码,但此问题中的这个无效。

** 我想要做的是使用 jquery 新闻收报机从文本文件中逐行读取并使用 jquery 向上滚动。但我认为这个“ split ”错误造成了无法让它工作的问题。

编辑**

我正在阅读的文本文件内容格式示例:

Hello this is a line
דווח במקור בתאריך: 22.03.14  שעה : 20:11

Hello this is a line too
דווח במקור בתאריך: 22.03.14  שעה : 22:59

每两行就像一个 block ,所以我想每次向上滚动时显示两行,然后每两组两行之间有一个空行/空格。

最佳答案

我不知道为什么您期望 txt 有一个表示文本的 .responseText 属性,但您应该只使用

    var lines = txt.split("\n");

关于javascript - 尝试使用 javascript 从文本文件读取文本,但方法 : split 出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22621982/

相关文章:

javascript - sails.js - 布局可以知道调用的 Controller / View 吗?

javascript - 网页不会自动调整到任何屏幕高度

javascript - Firefox AttachEvent 和 addEventListener 均存在问题

javascript - 在 for 循环中在对象中添加 +1 键

javascript - tinyMCE 添加多个按钮

javascript - 如何重试 xhr 请求,该请求在状态 0 上递归返回至少 n 次 promise

javascript - Bootstrap 绝对定位文本区域中的链接

javascript - 与 .svg 图像交互

javascript - 如何通过单击按钮打开 Accordion 菜单?

javascript - Discord 机器人无法使用管理员权限