How can I set the text and color of the event target in Meteor?

南笙酒味 提交于 2019-12-13 21:06:52

问题


I am loading an array of items in a Meteor template like so:

{{#each person}}
  <tr>
    <td><input type="checkbox" class="sendtextckbx" checked></td>
    <td>{{per_firstname}}</td>
    <td>{{per_lastname}}</td>
    <td>{{per_streetaddr1}}</td>
    <td>{{per_streetaddr2}}</td>
    <td>{{per_placename}}</td>
    <td>{{per_stateorprov}}</td>
    <td>{{per_zipcode}}</td>
    <td>{{per_emailaddr}}</td>
    <td class="textnum">{{per_phone}}</td>
    <td class="textmorph">Not sent</td>
    <td>{{per_notes}}</td>
  </tr>
{{/each}}

I want to update the td with the id "textmorph" when it is clicked. It starts off containing the text "Not sent" but when the td is clicked, I want it to cycle through other text strings, and change color accordingly, too.

Since there are 1..N instances of this td, I can't just assign to it via the class value, as there will be many sharing the same class (one for each person).

I think I can get the current value via evt.target.value, but I don't know how to set the value. This is my "pseudocode" (also known as a wild guess):

Template.peopleGrid.events({
  'click #btnTextChecked': function() {
    alert('you clicked the btnTextChecked button');
  },
  'click #textmorph': function(evt) {
    var currentText = evt.target.value;
    alert(currentText);
    if (currentText === 'Not sent') {
      evt.target.value = 'Sent';
      evt.target.color = amber;
    }
    else if (currentText === 'Sent') {
      evt.target.value = 'Need Help';
      evt.target.color = red;
    }
    else if (currentText === 'Not sent') {
      evt.target.value = 'Are OK';
      evt.target.color = green;
    }
  }
});

...but I'm not confident it's even close to being what it should be. In fact, when I test it out, ~

So how can I act on the selected tds, changing the text and the color? The app runs with the code above, but gives me an "undefined" alert when I click the "Not sent" tds.


回答1:


You are not too far. In pure javascript, you actually get and set an element's content through its innerHTML property. As for the color, you set it through style.color. And use quotes around color names!

Therefore:

Template.peopleGrid.events({
  'click #btnTextChecked': function() {
    alert('you clicked the btnTextChecked button');
  },
  'click #textmorph': function(evt) {
    var clicked = evt.target;
    var currentText = clicked.innerHTML;
    alert(currentText);
    if (currentText === 'Not sent') {
      clicked.innerHTML = 'Sent';
      clicked.style.color = 'amber';
    }
    else if (currentText === 'Sent') {
      clicked.innerHTML = 'Need Help';
      clicked.style.color = 'red';
    }
    else if (currentText === 'Need Help') {
      clicked.innerHTML = 'Are OK';
      clicked.style.color = 'green';
    }
  }
});


来源:https://stackoverflow.com/questions/33226835/how-can-i-set-the-text-and-color-of-the-event-target-in-meteor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!