【问题标题】:javascript function DOM event listener using "this"使用“this”的javascript函数DOM事件监听器
【发布时间】:2016-08-09 15:10:14
【问题描述】:

我希望我的事件侦听器函数 isTwoCards 从调用它的对象中获取信息。这段代码给了我 element.getAttribute 不是函数

var gameboard = document.getElementById('game-board');
var cards = ['queen', 'queen', 'king', 'king'];
var cardsInPlay = [];

var isTwoCards = function(element){
    cardsInPlay.push(element.getAttribute('data-card'));
    if (cardsInPlay.length === 2){
        isMatch(cardsInPlay);
        cardsInPlay = [];
    }
}
var createBoard = function(){
    for (var i = 0; i < cards.length; i++){
        cardDiv = document.createElement('div');
        cardDiv.setAttribute('class', 'card');
        cardDiv.setAttribute('data-card', cards[i]);
        cardDiv.setAttribute('id', i);
        cardDiv.addEventListener('click', isTwoCards(this));
        gameboard.appendChild(cardDiv);
    }
}
createBoard()

【问题讨论】:

  • cardDiv.addEventListener('click', isTwoCards); 然后在isTwoCards() 做:cardsInPlay.push(this.getAttribute('data-card'));
  • 您不需要将其作为参数传递(这也不起作用)。将isTwoCards(this) 替换为isTwoCards,将element.getAttribute('data-card') 替换为this.getAttribute('data-card')
  • addEventListener('click', isTwoCards(this)) isTwoCards 函数,而不是将其添加为监听器。

标签: javascript dom


【解决方案1】:

您立即运行isTwoCards,而不是将其分配为回调。据我所知,您需要分配isTwoCards(而不是其结果!)作为回调 - 即cardDiv.addEventListener('click', isTwoCards)。您还应该使用this.getAttribute('data-card') 而不是elementthis 在函数作为回调触发时分配给元素,而不是在分配回调时。

【讨论】:

    【解决方案2】:

    前面提到的问题是您正在执行函数,而不仅仅是将其作为参数传递。看看这个例子:

    someDomElement.addEventListener('click', function() { })
    

    让我们将该函数存储在一个变量中,然后将其作为参数传递。

    const myFunction = function() { }
    someDomElement.addEventListener('click', myFunction)
    

    看,我只是将存储在变量中的函数作为参数传递。现在你正在做的是执行函数:

    const myFunction = function() { }
    someDomElement.addEventListener('click', myFunction())
    

    这就是它不起作用的原因。

    【讨论】:

      【解决方案3】:

      您实际上将undefined 附加为侦听器,因为它正在执行函数并且其返回值为undefined

      你可以通过创建一个闭包/简单地返回一个新函数来解决这个问题:

      var isTwoCards = function(element){
        return function () {
          cardsInPlay.push(element.getAttribute('data-card'));
          if (cardsInPlay.length === 2){
              isMatch(cardsInPlay);
              cardsInPlay = [];
          }
        };
      }
      

      你也可以简单地在监听器内部访问this来访问事件监听器绑定的元素:

      var isTwoCards = function(){
          var element = this;
          cardsInPlay.push(element.getAttribute('data-card'));
          if (cardsInPlay.length === 2){
              isMatch(cardsInPlay);
              cardsInPlay = [];
          }
      }
      

      【讨论】:

      • 此答案不能解决调用函数而不是将其添加为侦听器的问题。 cardDiv.addEventListener('click', isTwoCards(this));
      • 只有在isTwoCards 没有返回函数时才会出现问题。
      • 确实如此。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      相关资源
      最近更新 更多