我之前说过我对 JavaScript 和 HTML 很陌生。我正在创建一个小型搜索工具,但对于如何从 URL 获取文本并将其放入我的 JS 数组中,我感到非常困惑。
例如,假设 URL 是: http: //www.somethingrandom.com/poop
在那个 URL 中,有几个词:“something”、“everything”、“nothing”
从字面上看就是这样。它在 HTML 的 pre 标记中,仅此而已。
现在,我的 JS 代码,我希望它打开那个 URL,然后把这些词放在一个字符串/列表/数组中,不管怎样,它可以是任何东西,只要它可以发生,我以后可以进一步操作它。
到目前为止我有这个:
<html>
<head>
<script type = "text/javascript">
function getWords(){
var url = "http://www.somethingrandom.com/poop"
var win = window.open( url );
window.onload = function(){
var list = document.getElementsByTagName("pre")[0].innerHTML;
var listLength = list.length;
alert( listLength);
}
}
</script>
</head>
<body>
<button id="1" onClick="getWords();">Click Here</button>
</body>
</html>
但是它不起作用..我不知道为什么。:( 请帮忙。