最近我一直在尝试使用Scala/Lift/Comet/Ajax等技术。我遇到了一个问题,归结为以下几点:
概述
当特定事件发生时,我想要更新指定的
目前,在使用Lift框架时,我无法使其正常工作。
源文件
LIFT_PROJECT/src/main/webapp/static/mouseViewTest.html:
<?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>Test</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
// <![CDATA[
$(document).ready(function() {
updateOrCreateMouseDiv('123', 'coords')
});
function updateOrCreateMouseDiv(uniqueId, coords) {
if ($('#mouse_'+uniqueId).length == 0) {
$('body').append('<div id=' + uniqueId + '>' + coords + '</div>');
}
$('#mouse_'+uniqueId).html(coords)
}
// ]]>
</script>
</head>
<body></body>
</html>
错误
如果我直接在浏览器中打开上面的文件(file:///LIFT_PROJECT/src/main/webapp/static/mouseViewTest.html
),它可以工作,即创建了一个新的div
。
但是,如果我通过Lift/Jetty运行它(http://localhost:8080/static/mouseViewTest
),我会收到以下JavaScript错误:
Chrome:
Uncaught Error: NO_MODIFICATION_ALLOWED_ERR: DOM Exception 7
Firefox(Firebug):
An invalid or illegal string was specified" code: "12
在浏览器中比较源代码
当在浏览器中比较页面源代码时,我只能看到一个区别,即:Lift会在闭合的</body>
标签前添加以下JavaScript代码:
<script type="text/javascript" src="/ajax_request/liftAjax.js"></script>
<script type="text/javascript">
// <![CDATA[
var lift_page = "F320717045475W3A";
// ]]>
</script>
问题
有人知道为什么会出现这种情况吗?
如果我想将JavaScript代码移动到Scala文件中(使用Lift的JavaScript和jQuery支持),代码会是什么样子?
请注意:当我使用 Jq("body") ~> JqAppend()
来创建新的div时,它可以正常工作。 我只是不知道如何检查div id是否已存在。 这就是为什么我把代码移到模板中,打算使用Lift的Call
函数来执行JS函数。 那时候就出现了这些问题...
谢谢!