JavaScript和#x27$';是未定义的
我是一个非常初学者,我希望我的JavaScript代码在文本文档中随机选取一行,然后显示它。我已经在论坛上找到了答案,那就是:JavaScript和#x27$';是未定义的,javascript,get,Javascript,Get,我是一个非常初学者,我希望我的JavaScript代码在文本文档中随机选取一行,然后显示它。我已经在论坛上找到了答案,那就是: //This loads your file from somewhere $.get( "Premium.txt", function( data ) { //Split data by lines if its in a formatted format (like json you have to decode or parse the d
//This loads your file from somewhere
$.get( "Premium.txt", function( data ) {
//Split data by lines if its in a formatted format (like json you have to decode or parse the data)
var lines = data.split("/n");
//Random item number
var r = Math.floor(Math.random() * lines.length);
//Get random line
var line = lines[r];
console.log(line);
});
当我运行代码时,它说“$”是未定义的。我不知道该怎么办,我不知道“$”是什么意思。如果有人能帮忙的话。这段代码特别使用jQuery。要创建它,必须在页面中包含jQuery。更多信息请参见 但您不需要jQuery来执行GET,您可以使用内置于现代浏览器中的
fetch
:
fetch("Premium.txt")
.then(function(response) {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.text();
})
.then(function(data) {
//Split data by lines if its in a formated format (like json you have to decode or parse the data)
var lines = data.split("\n");
//Random item number
var r = Math.floor(Math.random() * lines.length);
//Get random line
var line = lines[r];
console.log(line);
})
.catch(function(error) {
// Handle/report the fact an error occurred
});
你问题中的代码只使用了ES5和更早版本中的JavaScript特性,所以我一直坚持上面的说法。但在现代环境中,您可以使用ES2015+的功能(尽管在这段简单的代码中差别很小),如下所示:
fetch("Premium.txt")
.then(response => {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.text();
})
.then(data => {
//Split data by lines if its in a formated format (like json you have to decode or parse the data)
const lines = data.split("\n");
//Random item number
const r = Math.floor(Math.random() * lines.length);
//Get random line
const line = lines[r];
console.log(line);
})
.catch(error => {
// Handle/report the fact an error occurred
});
还要注意的是,
“/n”
应该是“\n”
(/
和\
)在JavaScript中的意思非常不同),我在上面对其进行了更新。该代码特别使用jQuery。要创建它,必须在页面中包含jQuery。更多信息请参见
但您不需要jQuery来执行GET,您可以使用内置于现代浏览器中的fetch
:
fetch("Premium.txt")
.then(function(response) {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.text();
})
.then(function(data) {
//Split data by lines if its in a formated format (like json you have to decode or parse the data)
var lines = data.split("\n");
//Random item number
var r = Math.floor(Math.random() * lines.length);
//Get random line
var line = lines[r];
console.log(line);
})
.catch(function(error) {
// Handle/report the fact an error occurred
});
你问题中的代码只使用了ES5和更早版本中的JavaScript特性,所以我一直坚持上面的说法。但在现代环境中,您可以使用ES2015+的功能(尽管在这段简单的代码中差别很小),如下所示:
fetch("Premium.txt")
.then(response => {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.text();
})
.then(data => {
//Split data by lines if its in a formated format (like json you have to decode or parse the data)
const lines = data.split("\n");
//Random item number
const r = Math.floor(Math.random() * lines.length);
//Get random line
const line = lines[r];
console.log(line);
})
.catch(error => {
// Handle/report the fact an error occurred
});
还要注意的是,
“/n”
应该是“\n”
(/
和\
)在JavaScript中的意思非常不同),我在上面已经对它进行了更新。我不知道为什么,也许我没有按应该的方式运行代码,但它不知道“获取”是什么。它显示了一个未定义的错误:/我不知道为什么,也许我没有按应该的方式运行代码,但它不知道什么是“获取”。它显示一个未定义的错误:/那么$
是jQuery,所以您没有包含jQuery。我会用fetch。哦,顺便说一句,它应该是\n
,而不是/n
!尝试将此脚本添加到您的代码中。jquery.com/jquery-3.5.1.js“>好吧,$
是jquery,所以您没有包含jquery。我只会使用fetch。哦,顺便说一句,它将是\n
而不是/n
!尝试将此脚本添加到您的代码中。jquery.com/jquery-3.5.1.js”>如果您使用IE11,那么这可能就是原因,因为IE11不“知道”取数。您使用的浏览器或环境是什么?如果您使用IE11,那么这可能就是原因,因为IE11不“知道”获取。您使用的浏览器或环境是什么?