基本上我有一些HTML和Javascript旨在从网页上获取数据。使用jsfiddle,我知道带有id myscript的脚本会成功获取链接,以便它成为。
<script id="myscript" source="whats in the link">
因此该部分可以工作,但之后应该用于加载文件并从中获取由于某种原因无效的数据。 如果我已经设置了src的普通脚本标记(在我的代码中没有document.getElementById),那么它会成功加载并显示所需数据。 但不知何故,即使脚本src正在改变我想要的数据,也没有加载数据。
<script>
var yqlcallback = function(data) {
var results = data.query.results;
document.write(results.span);
var rating = results.span;
rating = rating.slice(0, -1);
document.write(rating);
};
</script>
<script id="myscript"></script>
<script>
var link = "https://query.yahooapis.com/v1/public/yql?q=select%20content%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.team-des-fra.fr%2FCoM%2Fbf3.php%3Fp%3Dbj9912%22%20and%20xpath%3D'%2F%2F*%5B%40id%3D%22content%22%5D%2Fdiv%5B3%5D%2Fdiv%2Fspan'&format=json&callback=yqlcallback";
document.getElementById('myscript').setAttribute('src', link);
</script>
答案 0 :(得分:0)
检查控制台会显示此错误
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
This similar question has the reason why this error is thrown
使用innerHTML作为adeneo在他的jsFiddle中所做的似乎是有用的。