Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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 onclick对象相关方法_Javascript_Object_Methods_Onclick - Fatal编程技术网

Javascript onclick对象相关方法

Javascript onclick对象相关方法,javascript,object,methods,onclick,Javascript,Object,Methods,Onclick,我得到了一个切换onclick函数的图像。调用的函数是对象的方法。 我的问题是:我不知道对象的名称,所以如何才能 初始HTML代码: <img id="someID" [...] onClick="someObject.aFunction();" /> 不是很好,但目前还有效。有更好的解决方案吗?这里有一个可能的解决方案: var el = document.getElementById('someID'); el.onclick = function(){ // o

我得到了一个切换onclick函数的图像。调用的函数是对象的方法。 我的问题是:我不知道对象的名称,所以如何才能

初始HTML代码:

<img id="someID" [...] onClick="someObject.aFunction();" />

不是很好,但目前还有效。有更好的解决方案吗?

这里有一个可能的解决方案:

var el = document.getElementById('someID');
el.onclick = function(){  
     // or some other prop
     if(el.hasBeenClicked)
         function1();
     else
         function2();

     // toggle the selected state
     el.hasBeenClicked= !el.hasBeenClicked;
}  
当然,有很多(更好的)解决方案。
如果使用jquery,则代码应翻译为以下内容:

$('#someID').toggle(function1, function2);

这里有一个可能的解决方案:

var el = document.getElementById('someID');
el.onclick = function(){  
     // or some other prop
     if(el.hasBeenClicked)
         function1();
     else
         function2();

     // toggle the selected state
     el.hasBeenClicked= !el.hasBeenClicked;
}  
当然,有很多(更好的)解决方案。
如果使用jquery,则代码应翻译为以下内容:

$('#someID').toggle(function1, function2);
你可以试试这个

someObject.doFunction = function() {
    if (this.currentFunction == this.aFunction)
        this.currentFunction = this.otherFunction;
    else
        this.currentFunction = this.aFunction;
    this.currentFunction();
}

function h() { someObject.doFunction()}

var el = document.getElementById('someID');
if (el.attachEvent) {
    el.attachEvent("onclick", h);
} else {
    el.addEventListener("click", h, false); 
}
你可以试试这个

someObject.doFunction = function() {
    if (this.currentFunction == this.aFunction)
        this.currentFunction = this.otherFunction;
    else
        this.currentFunction = this.aFunction;
    this.currentFunction();
}

function h() { someObject.doFunction()}

var el = document.getElementById('someID');
if (el.attachEvent) {
    el.attachEvent("onclick", h);
} else {
    el.addEventListener("click", h, false); 
}