Uncaught SyntaxError: Failed to set the 'innerHTML' property on 'Element'

本文介绍了一个关于XHTML文档使用jQuery Mobile时出现的错误,并提供了详细的解决方案。问题出现在尝试修改XHTML文档的内容时,由于XHTML不支持innerHTML等方法导致。文章最后给出了修正方法,通过设置data-ajax属性为false解决了此问题。

环境:win7 64位,apache,chrome 44.0.2403.125

写了下面一个xhtml文件

<?xml version="1.0" encoding="UTF-8"?>
<html xml:lang="zh-cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:epub="http://www.idpf.org/2007/ops">
	<head>
		<meta charset="UTF-8"/>
		<meta name="viewport" content="width=768, height=1024"/>
		<script type="text/javascript" src="scripts/jquery.js"/>
		<script type="text/javascript" src="scripts/jquery.mobile.min.js"/>
		<link rel="stylesheet" type="text/css" href="./scripts/jquery.mobile.min.css"/>
	</head>
	<body data-width="768" data-height="1024">
		<a style="z-index:1;" href="chapter-001.xhtml">abcd</a>
	</body>
</html>

点击链接时报错:

Uncaught SyntaxError: Failed to set the 'innerHTML' property on 'Element': The provided markup is invalid XML, and therefore cannot be inserted into an XML document.

出错行是jquery.mobile-1.4.2.js的4911行

//workaround to allow scripts to execute when included in page divs
all.get( 0 ).innerHTML = html;

原因:

XHTML does not support document.write or .innerHTML. Due to the fact, that jQuery inserts the new code using one of these methods, all XHTML compatible browsers will error out.

XHTML with application/xhtml+xml does not support raw modification of the source using any of these jQuery methods: .append(), .html(), .insert...(), etc.

Instead, query for some JSON data and insert the AJAX-received values into either pre-defined tags using .text() / .val() or dynamically create those nodes using document.createElement('someTag').

https://www.w3.org/MarkUp/2004/xhtml-faq#docwrite

解决:

在a标签里添加

data-ajax="false"

即:

<a style="z-index:1;" href="chapter-001.xhtml" data-ajax="false">abcd</a>

参考:

http://stackoverflow.com/questions/27824609/uncaught-syntaxerror-failed-to-set-the-innerhtml-property-on-element-the-p

http://blog.youkuaiyun.com/coolcoffee168/article/details/24966689

在使用 `Element.prototype.querySelector` 方法时,出现 `Uncaught SyntaxError: Failed to execute 'querySelector' on 'Element':` 错误通常意味着传入的 CSS 选择器字符串存在语法问题。`querySelector` 要求传入的是一个符合 CSS 选择器规范的字符串表达式。 ### 常见原因及解决方法 #### 1. **无效的选择器字符** 如果选择器中包含特殊字符(如 `#`, `.`, `[`, `]`, `:`, `=` 等),而没有正确转义或格式化,可能导致解析失败。例如,以数字开头的 ID 名称虽然在 HTML 中是允许的,但在某些浏览器实现中可能不被推荐使用,尤其是在未加引号或未正确处理的情况下。 ```javascript const element = document.querySelector('#09110201'); // 正确写法 ``` 如果仍然报错,可以尝试使用属性选择器替代: ```javascript const element = document.querySelector('[id="09110201"]'); // 替代写法[^1] ``` #### 2. **动态拼接选择器时未正确处理变量** 在动态构建选择器字符串时,若未对变量进行适当的转义或格式化,也可能导致非法字符被带入选择器中。 ```javascript const id = '09110201'; const element = document.querySelector(`#${id}`); // 使用模板字符串安全拼接 ``` #### 3. **使用了非字符串类型的参数** 确保传入 `querySelector` 的参数是一个字符串类型。如果传入的是变量,需确认其值是否为字符串且格式正确。 ```javascript let selector = '#myId'; const element = document.querySelector(selector); ``` 如果传入了对象、数字或其他类型,会抛出类型错误。 #### 4. **嵌套调用中的上下文问题** `querySelector` 可以作用于任意 `Element` 对象,而不仅限于 `document`。但需要注意上下文元素的有效性。 ```javascript const parent = document.getElementById('parent'); const child = parent.querySelector('.child'); // 在父元素内部查找子元素 ``` 如果 `parent` 本身为 `null` 或不是有效的 DOM 元素,则可能导致错误。 #### 5. **使用伪类或伪元素选择器时超出限制** 部分伪类(如 `:hover`, `:focus`)或伪元素(如 `::before`, `::after`)无法通过 `querySelector` 获取。 ```css /* 以下将失败 */ document.querySelector('div::before'); ``` 这些伪元素不属于文档树结构,因此不能作为查询目标。 --- ### 最佳实践建议 - 使用 `try...catch` 捕获潜在异常,避免脚本中断执行。 - 在开发阶段使用开发者工具检查生成的选择器字符串是否合法。 - 对用户输入或动态生成的选择器进行合法性校验和清理。 - 若频繁操作 DOM,可考虑使用现代框架(如 React、Vue)提供的组件化机制降低直接操作 DOM 的风险。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值