从我的图像中删除onclick
Removing onclick from my images
所以我有以下javascript代码。基本上,这是在创造一个在线版本的纸牌游戏"记忆"。你有隐藏的图像在网格中,你翻转一个,需要找到它的匹配图像)。一切对我来说都很好,除了我的图像匹配之后。如果它们匹配,我想从图像中删除onclick事件,以防止用户再次单击它。然而,我所寻找和尝试的一切都对我不起作用。
我的部分需求是"将firstFlip和secondFlip的onclick事件处理程序更改为一个除了返回值false之外什么都不做的匿名函数"。下面是我用来删除事件的代码片段(checkTiles()函数的一部分):
firstFlip.onclick = function(){return false;};
secondFlip.onclick = function(){return false;};
下面是整个javascript代码:
/*
Filename: tiles.js
Global Variables
flipCount
Used to track the number of tiles currently being turned over
firstFlip
Used to reference the first tile turned over
secondFlip
Used to reference the second tile turned over
Functions
addEvent(object, evName, fnName, cap)
Run the function fnName when the event evName occurs in object.
randomSort(arr)
Randomly sorts the contents of the arr array.
setOpacity(object, value)
Sets the opacity level of object to value
setupTiles()
Sets up the tiles for use in the Concentration game
flipTile()
Flips a tile showing the image associated with the tile
checkTiles(tile1, tile2)
Checks whether the tile1 image source is the same as the
tile2 image source
flipBack()
Flips back flipped over tiles and resets the flipCount
variable to 0.
*/
var flipCount = 0;
var firstFlip = new Image();
var secondFlip = new Image();
addEvent(window,"load", setupTiles, false);
function setupTiles() {
var tiles = new Array(); // array of tile images
for (i = 0;i < document.images.length; i++) { // go through all images
image = document.images[i];
if (image.className == "tile" && image.parentNode.tagName == "A") { // must be of class tile and enclosed in <a></a>
tiles.push(image); // add tile to array
}
}
var tileImages = new Array(tiles.length); // array holding actual tile images
for(j = 0; j < tileImages.length/2; j++) {
var picture = new Image();
picture.src = "tileImage" + j + ".jpg";
tileImages[j] = picture;
}
for(n = tileImages.length/2; n < tileImages.length; n++) {
picture = new Image();
picture.src = "tileImage" + (n - tileImages.length/2) + ".jpg";
tileImages[n] = picture;
}
randomSort(tileImages); // randomly position the images in the array
for(m = 0; m < tiles.length; m++) {
tiles[m].flipped = tileImages[m]; // custom property to reference flipped image
addEvent(tiles[m], "click", flipTile, false); // run flipTile whenever image is clicked
}
showAll = document.getElementById("showAll");
showAll.onclick = function() {
for(h = 0; h < tiles.length; h++)
tiles[h].src = tiles[h].flipped.src;
};
reload = document.getElementById("reload");
reload.onclick = function() {
location.reload();
};
}
function flipTile() {
if(flipCount == 0) {
this.src = this.flipped.src;
firstFlip = this;
flipCount++;
}
else if(flipCount == 1) {
this.src = this.flipped.src;
secondFlip = this;
flipCount++;
checkTiles();
}
}
function checkTiles() {
if (firstFlip.src != secondFlip.src) {
setTimeout(flipBack, 800);
}
else {
flipCount = 0;
setOpacity(firstFlip, 70);
setOpacity(secondFlip, 70);
firstFlip.onclick = function(){return false;};
secondFlip.onclick = function(){return false;};
}
}
function flipBack() {
firstFlip.src = "tile.jpg";
secondFlip.src = "tile.jpg";
flipCount = 0;
}
function addEvent(object, evName, fnName, cap) {
if (object.attachEvent)
object.attachEvent("on" + evName, fnName);
else if (object.addEventListener)
object.addEventListener(evName, fnName, cap);
}
function randomSort(arr) {
arr.sort(function () {
return 0.5 - Math.random();
})
}
function setOpacity(object, value) {
// Apply the opacity value for IE and non-IE browsers
object.style.filter = "alpha(opacity = " + value + ")";
object.style.opacity = value/100;
}
看起来你可以简单地添加一个removeEvent方法:
function removeEvent(object, evName, fnName) {
if (object.detachEvent)
object.detachEvent("on" + evName, fnName);
else if (object.removeEventListener)
object.removeEventListener(evName, fnName);
}
然后用image元素和flipTile
函数作为参数调用:
removeEvent(firstFlip, 'click', flipTile);
相关文章:
- .hasClass 在 onclick 函数中不起作用,但删除和添加类函数可以
- "/"当onclick事件时从URL中删除
- 如何为span元素添加/删除类onclick
- 如何使用onclick javascript动态添加,删除列表中的节点
- 删除/隐藏 javascript 中的选择选项而不会使其看起来很尴尬(需要延迟 select 元素的本机 onclick
- Onclick 事件以删除文本输入字段中的默认值
- Jquery 日期选择器删除 onclick 和 onchange 并添加默认属性
- 如何删除出现在弹出窗口上的onclick按钮-CSS&JS相关
- 添加/删除类onclick with JavaScript no librarys
- 删除部分除法的onclick侦听器
- jQuery Mobile onclick事件不会删除已禁用的类ui
- 使用JavaScript onclick删除元素,并在SQL中保存变量
- jQuery验证:删除附近的错误消息onClick
- 通过onclick添加/删除元素
- HTML、CSS和Javascript:如何使onclick甚至删除输入文本字段中的文本
- 当使用javascript onclick I'我在IE中得到了一个小链接图标,我该如何删除它
- 为什么我的onclick不删除我的任何类
- jQuery自定义菜单-添加和删除类onClick
- 从动态元素中删除onClick
- 从我的图像中删除onclick