如何在Meteor.js中设置html/body标签属性?

问题描述 投票:0回答:3

我需要在 Meteor.js 应用程序中的 html 标签或文档的 body 标签上设置属性。

具体来说,我想要

<html dir="rtl">
<body dir="rtl">
..

尝试后者,我收到控制台消息:

While building the application:
client/views/layout/layout.html:7: Attributes on <body> not supported

=> Your application has errors. Waiting for file change.

那么你如何做到这一点?

meteor
3个回答
19
投票

您必须在启动时将它们注入到客户端 Javascript 中:

Meteor.startup(function() {
   $('html').attr('dir', 'rtl');
});

更新

请注意,您现在可以为 body 标记内嵌设置属性,它们将通过 Meteor 以与 body 标记的内容相同的方式连接:

<body data-atttribute="foobar"></body>

您可以有多个不同的正文标签,它们将被组合起来,因此上面的代码只会向您现有的正文添加一个属性,而不是替换它。

据我所知,HTML 标签属性仍然需要通过 Javascript 设置。


9
投票

可以使用

startup
功能在
WebApp.addHtmlAttributeHook
上设置HTML标签的属性。这是一个例子:

Meteor.startup(function() {
    WebApp.addHtmlAttributeHook(function() {
        return {
            "dir": "rtl"
        }
    })
});

确保您在服务器上调用此函数,而不是客户端。


0
投票

如果其他人偶然发现这一点并意识到上述解决方案不再有效,以下是对我来说 Meteor 2.13 有效的方法:

Meteor.startup(() => {
  document.documentElement.setAttribute('dir', 'rtl');
});

在这里找到它:https://github.com/meteor/meteor-feature-requests/issues/322

© www.soinside.com 2019 - 2024. All rights reserved.