从Javascript文件(流星)访问HTML DOM

Access HTML DOM from Javascript file (Meteor)

本文关键字:访问 HTML DOM 流星 Javascript 文件      更新时间:2023-09-26

我不确定这是否是一个好问题,甚至是一个正确的方法,但我只是想知道我如何可能从我的JS文件使用流星禁用按钮。

我尝试

$("#button").disabled = true;

它没有工作。我不确定这是否是正确的方法。我想也许在我的HTML文件中创建一个脚本标签下的函数,但我认为考虑到我有一个代表我的模型的JS文件,这似乎是多余的,所以我只是试图弄清楚我是否可以从该文件访问HTML标签。

代码如下:

Users = new Mongo.Collection("user-info");
if (Meteor.isClient) {
  var myApp = angular.module('calorie-counter',['angular-meteor']);
  myApp.controller('formCtrl',['$scope',function($scope) {
  $("#button").disabled=true;
  $scope.calories;
  $scope.goal;
  $scope.user;
  $scope.submit = function() {
    Meteor.call("submit",$scope.user);
    $scope.clear();
  }
  $scope.clear = function() {
    $scope.user = {
      item1:'',
      item2:'',
      calories:'',
      goal:''
    };
  }
 }]);
}

首先,我不是angularjs的用户。但是你的代码必须运行模板渲染之后。试着像下面这样修改代码。(注意:"yourTemplate"改为你的)

Users = new Mongo.Collection("user-info");
if (Meteor.isClient) {
  Template.yourTemplate.onRendered(function() {
    var myApp = angular.module('calorie-counter', ['angular-meteor']);
    myApp.controller('formCtrl', ['$scope',
      function($scope) {
        $("#button").disabled = true;
        $scope.calories;
        $scope.goal;
        $scope.user;
        $scope.submit = function() {
          Meteor.call("submit", $scope.user);
          $scope.clear();
        }
        $scope.clear = function() {
          $scope.user = {
            item1: '',
            item2: '',
            calories: '',
            goal: ''
          };
        }
      }
    ]);
  });
}