【问题标题】:TypeError: param.toElement is undefined类型错误:param.toElement 未定义
【发布时间】:2014-10-17 11:59:49
【问题描述】:

大家好,我有以下混合了 Prototype JS 和 Jquery 的代码:

var oldZindex = 0;
var theClickID = null;

UIProgressButton.prototype._initEvents = function() {
    var self = this;
    this.button.addEventListener( 'click', function(param) {
        $('#overlay').css({'visibility':'visible'});
        theClickID = $('button[data-id="' + param.toElement.attributes[1].value + '"]').parent().attr('id');
        oldZindex = $('#' + theClickID).css('z-index');
    });
}

此代码在 Chrome 浏览器中运行良好,但似乎抛出以下错误:

typeerror param.toelement 未定义

在 FireFox 中.... 我知道它确实可以工作,因为它在 Chrome 中工作得很好,我该如何解决这个问题?

【问题讨论】:

    标签: javascript jquery firefox prototypejs


    【解决方案1】:

    通过添加以下内容来修复它:

    var Element = ((window.event)?(event.srcElement):(evt.currentTarget));
    

    然后在我的代码中使用它:

    UIProgressButton.prototype._initEvents = function() {
        var self = this;
        this.button.addEventListener( 'click', function(param) {
            $('#overlay').css({'visibility':'visible'});
            var Element = ((window.event)?(event.srcElement):(param.currentTarget));            
            theClickID = $('button[data-id="' + Element.attributes[0].value + '"]').parent().attr('id');
            oldZindex = $('#' + theClickID).css('z-index');
        });
    }
    

    【讨论】:

      【解决方案2】:

      既然你已经有了 jQuery,你可以用它来绑定你的事件监听器,这样你在获取事件时就不会遇到 IE 的麻烦:

      var oldZindex = 0;
      var theClickID = null;
      
      UIProgressButton.prototype._initEvents = function() {
          var self = this;
          $(this.button).on( 'click', function(param) {
              $('#overlay').css({'visibility':'visible'});
              theClickID = $('button[data-id="' 
                //you may prefer to get a named attribute
                //instead of by index:
                //param.getAttribute('attributeName')
                + param.target.attributes[1].value 
                + '"]').parent().attr('id');
              oldZindex = $('#' + theClickID).css('z-index');
          });
      }
      

      【讨论】:

        猜你喜欢
        • 2019-10-31
        • 2016-08-07
        • 2019-01-07
        • 2020-01-28
        • 2021-10-24
        • 2022-01-10
        • 2019-12-24
        • 2020-02-27
        • 2012-09-12
        相关资源
        最近更新 更多