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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境: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

你的问题与代码错误相关,我将尝试帮助你解决。 从错误信息`VM795:33 Uncaught SyntaxError: Failed to execute 'appendChild' on 'Node': Illegal return statement`来看,这是一个语法错误,提示你在某个地方尝试使用`appendChild`方法时出现了问题。 在JavaScript中,`appendChild`方法通常用于将一个节点添加到DOM树中的某个位置。这个方法通常不返回任何值,因此不应该在`appendChild`方法内部使用return语句。如果你在`appendChild`方法内部使用了return语句,就会导致这个错误。 为了解决这个问题,你需要检查代码中调用`appendChild`方法的代码块,并确保它符合JavaScript的语法规则。一般来说,你可以使用浏览器的开发者工具来检查具体的代码位置,找到可能出错的地方。 以下是一个可能的解决方案,但请注意,这只是一个示例,你需要根据实际情况进行修改: ```javascript // 假设你要将一个节点添加到某个父节点下 var parentNode = document.getElementById('parent'); // 获取父节点 var newNode = document.createElement('div'); // 创建一个新的节点 // 确保在appendChild方法外部没有使用return语句 // ...你的代码... parentNode.appendChild(newNode); // 将新节点添加到父节点下 ``` 请注意,我提供的解决方案是基于常见的情况和错误信息。如果你能提供更多的代码或上下文信息,我可以更准确地帮助你解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值