简体   繁体   English

删除按钮的onClick之后,我正尝试将其恢复原状,但出现了一个有趣的问题

[英]After removing an onClick of a button, i'm trying to bring it back but i'm having an interesting issue

So i'm using 所以我在用

   tree = document.getElementById("treeButton");
tree.onclick = function() {
      tree.onclick = null;
}

   treeTwo = document.getElementById("treeButtonTwo");
treeTwo.onclick = function() {
      treeTwo.onclick = null;
}

To make sure these buttons (which are actually images with onclicks) can't be clicked. 为了确保这些按钮(实际上是带有onclicks的图像)不能被单击。 This works just fine and dandy but bringing back the onclick has been tricky! 这个工作很好而且很花哨,但是带回onclick却很棘手!

function clickableTree(){
    tree.onclick = cutTree(1);
    treeTwo.onclick = cutTree(2);
}

The above code brings back the onclicks to the images, which they do terrifically! 上面的代码将onclicks带回了图像,这是他们很费力的! But when it does this it also calls the function cutTree(number); 但是,当执行此操作时,它还会调用函数cutTree(number);。 which is absolutely not what I want to happen. 这绝对不是我想要发生的事情。 Before I have this fixed by just saying tree.onclick = cutTree; 在我通过说出tree.onclick = cutTree;来解决此问题之前, but now there are two trees and the number needs to be there to allow the function to know which tree to take care of. 但是现在有两棵树,并且需要有几棵树,以便函数知道要照顾哪棵树。

Is there any way to not get it to call to the function? 有没有办法让它不调用该函数?

Try this out, per my comment above. 根据我上面的评论尝试一下。

tree.onclick = function() { cutTree(1); }
treeTwo.onclick = function() { cutTree(2); }

I think wrapping them in an anonymous function will prevent the immediate call of the function upon reassignment...? 我认为将它们包装在匿名函数中将防止在重新分配时立即调用该函数...?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM