jQuery无法用meteor.js更改DOM?

时间:2022-03-10 18:39:33

I'm using Meteor.js for a website I'm working on. I can't seem to use javascript (or jQuery) to manipulate the DOM at all.

我正在使用Meteor.js作为我正在研究的网站。我似乎无法使用javascript(或jQuery)来操纵DOM。

The code below does not change the #testDiv HTML, and there is nothing in the alert box.

下面的代码不会更改#testDiv HTML,并且警报框中没有任何内容。

Tracker.autorun(function(){
        $("#testDiv").text("test");
        alert($("#testDiv").text());
});

My HTML is very basic:

我的HTML非常基础:

    <div id="testDiv">
         this is a test
    </div>

Does anybody know whats going on? Is this a problem with my jQuery, or Meteor.js? Or am I just overlooking something simple?

有谁知道最近发生了什么?这是我的jQuery或Meteor.js的问题吗?或者我只是忽略了一些简单的事情?

1 个解决方案

#1


An autorun only executes when its reactive variables change. The best way to directly manipulate the DOM in meteor is in an onRendered callback. For example:

自动运行仅在其反应变量发生变化时执行。在meteor中直接操作DOM的最佳方法是在onRendered回调中。例如:

Template.myTemplate.onRendered(function() {
  $("#testDiv").text("test");
  alert($("#testDiv").text());
});

#1


An autorun only executes when its reactive variables change. The best way to directly manipulate the DOM in meteor is in an onRendered callback. For example:

自动运行仅在其反应变量发生变化时执行。在meteor中直接操作DOM的最佳方法是在onRendered回调中。例如:

Template.myTemplate.onRendered(function() {
  $("#testDiv").text("test");
  alert($("#testDiv").text());
});