我正在尝试从用户提供的外部链接中读取 .txt 文件,以便稍后在应用程序中使用它们。现在我只是想将它们显示在一个 div (.output) 中。到目前为止,我已经做了一些工作,但现在我卡住了,不知道该如何继续。
function getText(url){
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.send(null);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
var type = request.getResponseHeader('Content-Type');
if (type.indexOf("text") !== 1) {
return request.responseText;
}
}
}
}
$(".url-input").change(function() {
getText($(".url-input").value);
});
.output {
width:500px;
height:500px;
border: 1px solid black;
}
<!DOCTYPE html>
<html>
<head>
<title>Ugh</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<div class="output"></div>
<input class="url-input" type="url">
</body>
</html>
$(".url-input").value
是错误的,value
是一个 DOM 属性。请使用 jQuery 的.val()
或使用本地 DOM 对象。 - Sterling Archer