在同一个标​​签内有2x onClick

时间:2013-05-22 02:32:23

标签: javascript onclick

这可能与'事件冒泡'有关,但我怎么能在同一个标​​签中有2个onClick事件(1个展开img,1个回原始尺寸)?

e.g

<img class="a1" width="140" height="140" onClick="this.width=420;this.height=560;" onClick="this.width=140;this.height=140;" />

现在,第二个onClick不起作用。

提前致谢!

2 个答案:

答案 0 :(得分:0)

不是javascript,但你会明白

onclick(){
    isClicked ^= true

    if(isClicked){
        //do something
    }
    else{
        //do something else
    }
}

答案 1 :(得分:0)

你不能为同一个标签提供两个相同的属性,但你可以用你想要的方式做到这一点(未测试... = D):

<强> JS

window.addEventListener("load", function(){
  var img = document.getElementById("your_img");
  img.onclick = function(){
    var size = this.getAttribute("data-size");
    if(size === "big"){
      this.style.width = "140px";
      this.style.height = "140px";
      this.setAttribute("data-size", "small");
    } else {
      this.style.width = "560px";
      this.style.height = "420px";
      this.setAttribute("data-size", "big");
    }
  }
});

<强> HTML

<img id="your_img" src="anything.png" />

<强> CSS

#your_img{
  height:140px;
  width:140px;
}