Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编写基本原型脚本时,javascript如何检查事件类型?_Javascript_Events_Prototype - Fatal编程技术网

编写基本原型脚本时,javascript如何检查事件类型?

编写基本原型脚本时,javascript如何检查事件类型?,javascript,events,prototype,Javascript,Events,Prototype,以下是我的html代码结构: <dl id="J_tab"> <dt> <a href="#tab1">tab1</a><a href="#tab2">tab2</a><a href="#tab3">tab3</a> </dt> <dd id="tab1"> .. </dd> <dd id="

以下是我的html代码结构:

<dl id="J_tab">
    <dt>
        <a href="#tab1">tab1</a><a href="#tab2">tab2</a><a href="#tab3">tab3</a>
    </dt>
    <dd id="tab1">
      ..
    </dd>
    <dd id="tab2">
      ...
    </dd>
    <dd id="tab3">
      ..
    </dd>

</dl>
我不继续,因为我在检查事件类型时遇到问题。错误是类型未定义。如何使用代码检查事件类型

这个.checkType()可以像下面这样吗

if(event=="click" || event==null){
container.onclick=function(){


    }
}else if(event=="mouseover"){
container.onmouseover=function(){

}
}

onclick
事件处理程序中是对DOM元素的引用,而不是对对象的引用

要访问对象,请将其存储在外部作用域的局部变量中:

init: function() {
    var self = this;

    this.tabContainer.onclick = function(event) {
        var type = self.checkType(event);
        // Keep up the good work...
    }
}
在下面的
检查类型中,只需使用以下命令:

checkType: function(event) {
    return (event || window.event).type;
}

另一方面,由于代码<> CuthType 中的代码太琐碎了,您可以考虑只将其全部内嵌:

init: function() {
    this.tabContainer.onclick = function(event) {
        var type = (event || window.event).type;
    }
}

除了约瑟夫的回答之外,还有一些东西

函数的
由函数的调用方式设置。如果使用以下命令将函数分配给DOM元素:

someElement.onclick = someObject.method;
然后该函数将(有效地)被调用为:

someElement.method();
方法中的
设置为
someElement
。当将函数指定为对象的属性,然后将函数作为对象的方法调用时,通常会发生同样的情况

对守则的一些评论:

  • 按照惯例,构造函数以大写字母开头(表示应使用
    new
    调用它们)

  • 使用函数声明而不是表达式可能更好(更清晰)

  • 我不知道为什么在原型上分配
    init
    方法,然后从构造函数中调用它(除非您打算动态更改方法,这似乎不是一个好主意)。它似乎只是把代码分成了两个地方,可以放在一个地方。只需将init函数作为代码放入构造函数中

  • e、 g:

    关于
    init
    函数和使用函数表达式:

    var foo = new Foo(); // Happy :-)
    
    var Foo = function(){
      /* init code */;
    }
    
    但是


    哎呀!我走错了路,这个功能的初衷是检查用户是否想要设置触发器选项(单击或鼠标悬停)。这个checkType()可以这样做吗?[this.tabContainer.(onclick或onmouseover)],而不是在onclick函数中检查类型。
    function Tab(id, anchorContainer, curClass) {
      this.curClass = curClass || 'current';
      this.tabContainer = document.getElementById(id);
      this.anchorContainer = this.tabContainer.getElementsByTagName(anchorContainer)[0];
      this.links = this.anchorContainer.getElementsByTagName('a');
    
      // Put other "init" code here
      // Use the generic instance name for the instance, not "self"
      var tab = this;
    
      // From here on, use 'tab' not 'this' to refer to the instance
      tab.tabContainer.onclick = function(event) {
         /* instance is 'tab', element is 'this' */
      }
    
      Tab.prototype = { /* ... */ };
    }
    
    var foo = new Foo(); // Happy :-)
    
    var Foo = function(){
      /* init code */;
    }
    
    var Foo = function(){
      this.init();
    }
    
    var foo = new Foo(); // Error: this.init is not a function (or similar)
    
    Foo.prototype = {
      init: function(){ /* init code */ }
    };