“未捕获的TypeError:content.getElementById不是函数”

洛洛

我已经尝试了很多,但是每次遇到此错误时:“ Uncaught TypeError:content.getElementById不是函数”(附加脚本)

帮助将不胜感激:D花了我几个小时,无法弄清楚

var newcode = "";
var oldcode = "";

function update(){
	var iframe = document.getElementById('if');
	var content = iframe.contentWindow.document.getElementsByTagName('html')[0].getElementsByTagName('body')[0];
	newcode = content.getElementById("code").innerHTML;
	
	
	
	if(newcode != oldcode){
		oldcode = newcode;
		alert("lol"+newcode+"lol");
		document.head.innerHTML += "<script>"+newcode;
	}
	
}

window.setInterval(function(){
	update();
}, 5000);
<html>
<head>
<title>Check</title>
</head>
<body>
<iframe id="if" src="myurl"></iframe>
</body>
</html>

一定的表现

getElementById只能在上调用document-不能在上调用HTMLElement(甚至不能在上调用HTMLBodyElement)。毕竟,ID在文档中应该是唯一的。改成

var newCode = iframe.contentWindow.document.getElementById('code').innerHTML;

因此这iframe.contentWindow.document是指iframe中的文档。

如果您只想从祖先节点中选择一个特定的ID而无需返回文档,则可以使用querySelector-仅在ID前面加上前缀#

var newcode = content.querySelector("#code").innerHTML;

(但是content,假设HTML有效,则无需首先导航到

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

未捕获的TypeError:(中间值)(...)不是函数

未捕获的TypeError:$ .ajax(...)。成功不是函数

未捕获的TypeError:$ .post不是函数

Highcharts:未捕获的TypeError:$(...)。highcharts不是函数

未捕获的TypeError:.slideToggle不是函数

jQuery:未捕获的TypeError:$(...)。click不是函数

未捕获的TypeError:$(...)。waterwheelCarousel不是函数

未捕获的TypeError:$(...)。code不是函数(Summernote)

未捕获的TypeError:.unshift不是函数

WordPress中的“未捕获的TypeError:$不是函数”

未捕获的TypeError:$(...)。stellar不是函数

未捕获的TypeError:$(...)。ready不是函数

未捕获的TypeError:$(...)。draggable不是函数

未捕获的TypeError:this.transitionTo不是函数

未捕获的TypeError:$(…).on不是函数

未捕获的TypeError:#<Object>不是函数

未捕获的TypeError:“下载”不是函数

未捕获(承诺)TypeError:生成不是函数

未捕获的TypeError:$(...)。pickadate不是函数

未捕获的TypeError:函数不是函数

Lodash,未捕获的TypeError:_.remove不是函数

未捕获的TypeError:对象(...)不是函数

未捕获的TypeError:$(...)。formSelect不是函数

未捕获的TypeError:document.getElementById不是函数

Javascript:未捕获的TypeError:不是函数

未捕获的TypeError:$(...)。selectize不是函数

JavaScript WordPress未捕获的typeerror $不是函数

未捕获的TypeError:$(...)。tableDnD不是函数

未捕获的 TypeError 对象不是函数