如何检查JavaScript生成的内容是否有效?例如,以下文档将通过静态超文本标记语言验证(例如validator.nu),不会产生任何JavaScript错误,并且将在浏览器中无故障地呈现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>test</title>
<script>
window.onload = function() {
var text = document.createTextNode('nested anchor');
var anchor = document.createElement('a');
var nested_anchor = document.createElement('a');
nested_anchor.appendChild(text);
anchor.appendChild(nested_anchor);
document.getElementsByTagName('body')[0].appendChild(anchor);
};
</script>
</head>
<body>
<p>test</p>
</body>
</html>但是,输出在另一个锚元素中包含一个锚,根据HTML5规范,这是无效的。有没有办法检查这类错误?
发布于 2011-10-13 17:39:56
您可以将生成的HTML作为字符串输出,将其嵌入到有效文档中,然后将其发送以进行验证。在您的示例中,使用jQuery var htmlText = $(anchor).html()。
或者更简单,输出整个生成的html console.log($("html").html())。
https://stackoverflow.com/questions/7752058
复制相似问题