本文介绍了Meteor.js:< script>标签在< body>内部不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

body标签内的简单脚本标签似乎不起作用。警报未在以下代码中触发:

A simple script tag inside the body tag doesn't seem to work. The alert doesn't get triggered in the code below:

<body>
   <script type="text/javascript">
      alert('Hello');
   </script>

      {{>main}}

</body>

知道为什么?

编辑:
刚尝试使用新的流星应用程序,仍然没有警告标记:

Just tried it with a fresh meteor app, no alert tag still:

<head>
  <title>test</title>
</head>

<body>

  <script type="text/javascript">
     alert('Hello');
  </script>

  {{> hello}}
</body>

<template name="hello">
  <h1>Hello World!</h1>
  {{greeting}}
  <input type="button" value="Click" />
</template>

奇怪的是当我复制粘贴html的源代码时,创建了一个新的html页面,并且警报会奏效。

Weird thing is when I copy paste the source of the html, made a new html page, and the alert will work.

Edit3:我在这里部署了这个应用程序:
你有警报框吗?

I deployed this app here: http://alert-in-body-test.meteor.com/Do you get an alert box?

推荐答案

这个问题在当前版本的Meteor(版本0.5.4)中仍然有用,所以我想描述如何在正文末尾包含脚本。

This question is still relevant in the current version of Meteor (version 0.5.4) so I wanted to describe how to include script at the end of the body.

要在正文末尾执行javascript,请注册Handlebars帮助程序并将相关代码放在那里,如下所示:

To execute javascript at the end of the body, register a Handlebars helper and put the relevant code there, like this:

在client.html中:

In client.html:

<body>
  {{renderPage}}

  {{afterBody}}
</body>

...

在客户端。 js:

if (typeof Handlebars !== 'undefined') {
  Handlebars.registerHelper('afterBody', function(name, options) {
    $('body').append('AFTER BODY');
  });
}

(有关为什么需要这样做的详细说明,请参阅Rahul对a的回答类似的问题:)

(For a great description of why this is required, see Rahul's answer to a similar question here: https://stackoverflow.com/a/14002991/219238 )

这篇关于Meteor.js:&lt; script&gt;标签在&lt; body&gt;内部不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:13