使用 Lift 将 div 添加到 HTML 正文

发布于 2024-09-13 08:12:07 字数 2416 浏览 4 评论 0原文

我最近一直在玩 Scala/Lift/Comet/Ajax 等。我遇到了一个问题,可以归结为:

摘要

我想在发生某个事件时更新特定的 div (按 id)。如果 div 尚不存在,则必须创建它并将其附加到 HTML 正文。

目前,我在使用 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>

///LIFT_PROJECT/src/main/webapp/static/mouseViewTest.html

如果我直接在浏览器中打开上述文件(file:)它起作用了,即创建了一个新的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 在结束 标记之前添加了以下 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函数。这就是这些问题开始的时候......

谢谢!

I have been playing around with Scala/Lift/Comet/Ajax etc. recently. I came across a problem which boils down to this:

Summary

I want to update a specific div (by id) when a certain event occurs. If the div does not exist yet, it must be created and appended to the HTML body.

Currently I cannot get this to work when using the Lift framework.

Source File

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>

The Error

If I open the above file directly in a browser (file:///LIFT_PROJECT/src/main/webapp/static/mouseViewTest.html) it works i.e. a new div is created.

But if I run it through Lift/Jetty (http://localhost:8080/static/mouseViewTest) I get the following JavaScript error:

Chrome:

Uncaught Error: NO_MODIFICATION_ALLOWED_ERR: DOM Exception 7

Firefox (Firebug):

An invalid or illegal string was specified" code: "12

Comparing the Sources in Browser

When comparing the page sources in the browser, I can see only one difference, namely: Lift adds the following JavaScript just before the closing </body> tag:

<script type="text/javascript" src="/ajax_request/liftAjax.js"></script>    
<script type="text/javascript">
// <![CDATA[
var lift_page = "F320717045475W3A";
// ]]>
</script>

Questions

Does anyone have an idea why this happens?

If I would want to move the JavaScript code into the Scala file (using Lift's JavaScript and jQuery support), what would the code look like?

Please note: When I used Jq("body") ~> JqAppend() to create new divs, it worked. I just didn't know how to check whether the div id already existed. Thats why I moved the code into the template, planning on using Lift's Call function to execute the JS function. And thats when these problems started...

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2024-09-20 08:12:07

我最近遇到了一个类似的问题,根据我收集的信息,问题是因为通过电梯提供服务的页面是 XHTML,如果页面是 XHTML 与 HTML,则在写入 DOM 时会出现一些问题。我不知道这是否是 jQuery 或 Safari 的错误,或者它是否只是 XHTML 中不可能的东西,但修复它的快速方法是修改 Boot.scala 以告诉 Lift 不要使用 XHTML 作为 mime 类型用这一行:

LiftRules.useXhtmlMimeType = false

I recently ran into a similar problem and, from what I've gathered, the problem is because the page when served by lift is served as XHTML and there are some issues when writing to the DOM if the page is XHTML vs. HTML. I don't know whether this is a bug with jQuery or Safari or if it's just something that's not possible in XHTML, but a quick way to fix it is to modify your Boot.scala to tell Lift to not use XHTML as the mime type with this line:

LiftRules.useXhtmlMimeType = false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文