解析属性名称时出错
Error parsing attribute name
浏览器说我的javascript在我的xhtml中这个函数中有一个错误。如果我删除此功能,错误就会消失。对我来说,这段代码在语法上看起来很完美:
function toAscii(text) {
for (var i=0; i<text.length; i++) {
var charCode = text.charCodeAt(i);
var ascii = charCode.toString(2);
console.log(ascii);
}
}
铬错误:
This page contains the following errors:
error on line 19 at column 31: error parsing attribute name
Below is a rendering of the page up to the first error.
火狐错误:
Erro no processamento de XML: formatação incorreta
Posição: file:///C:/Users/Carlos/Desktop/ascii%20converter.xhtml
Número da linha 19, coluna 33:
for (var i=0; i<text.length; i++) {
---------------------------------------------------^
这是整个XHTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ASCII Converter</title>
<meta charset="UTF-8"/>
<script type="text/javascript">
function converter() {
var text = document.getElementById('texto').value;
toAscii(text);
}
function toAscii(text) {
for (var i=0; i<text.length; i++) {
var charCode = text.charCodeAt(i);
var ascii = charCode.toString(2);
console.log(ascii);
}
}
</script>
</head>
<body>
Texto (Entrada):<br/>
<input id="texto" type="text"/><br/>
ASCII (Saída):<br/>
<input id="ascii" type="text"/><br/>
<input type="button" id="botao" value="Converter" onclick="converter()"/><br/>
</body>
</html>
由于您使用的是 xhtml 并且您的代码中有一个<
,因此您必须将代码包含在 cdata 块中
<script type="text/javascript"><![CDATA[
function converter() {
var text = document.getElementById('texto').value;
toAscii(text);
}
function toAscii(text) {
for (var i=0; i<text.length; i++) {
var charCode = text.charCodeAt(i);
var ascii = charCode.toString(2);
console.log(ascii);
}
}
]]></script>
严格来说,XHTML不允许javascript像HTML那样卡在标头中。正如有人提到的,它应该包装在 CDATA 部分中。作为旁注,您发布的代码在我的浏览器上运行,但它可能是宽松的。查看此参考以获取有关 xhtml 和嵌入式 css/javascript 的更多信息:
https://developer.mozilla.org/en-US/docs/Properly_Using_CSS_and_JavaScript_in_XHTML_Documents
相关文章:
- AngularJS指令出错-无法读取属性'编译'的未定义
- 解析 /page.xhtml 时出错:跟踪错误[行: 42] 与元素类型“id”关联的属性“{1}”应使用左引号
- 为JavaScript类属性赋值时出错
- 访问console.log中的数据属性时出错
- 在Spring.message属性中添加新行时出错
- Javascript:“;无法读取属性'addEventListener'“为空”;尝试删除子项时出错
- '无法设置未定义'的属性;应用绑定时出错
- 使用Internet Explorer进行react redux trySubscribe时出错(无法获取未定义的属性“
- 在 Javascript 中使用警报时出错(对象的属性“alert”不是函数)
- css 可见性属性和 javascript setTimeout 方法出错
- 加载图表时出错 未捕获的类型错误:无法读取未定义的属性“到小写”
- 在网格视图中获取属性“行”时出错
- Metro Javascript.对项目进行分组时出错.ui.js firstItemIndexHint 属性
- 解析属性名称时出错
- Ember Data 1.0 加载路由时出错:类型错误:无法设置未定义的属性“类型键”
- 将 req.body 属性复制到猫鼬模型中时出错
- 尝试在火狐扩展中获取标题属性时出错
- JS:防止访问未定义对象的属性时出错
- 避免'无法读取未定义'的属性;映射数据时出错
- TYPO3 Neos:Apply content属性在JavaScript中运行时出错