我读了这个帖子并尝试使用Ted Hopps方法 Function in javascript that can be called only once
但似乎我不能让它正常工作。我想通过onclick =“function()”在图像上创建一个带有JS的div。但我只想这样做一次。
function runOnce(f){
var executed = false;
return function () {
if (!executed) {
executed = true;
return f.apply(this,arguments);
}
};
}
function rollover() {
var new1=runOnce(myfunction);
new1(1);
}
function myfunction(){
var tr1 = document.createElement("tr1");
tr1.style.width = "50%";
tr1.style.height = "100%";
tr1.style.background = "red";
tr1.style.color = "white";
tr1.style.right= "0px"
tr1.style.top= "0px"
tr1.style.padding= "2%"
tr1.style.opacity= "0.6"
tr1.style.position = "fixed";
tr1.innerHTML = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
document.body.appendChild(tr1);
}
我从onclick
调用翻转功能<img class="trans01button" onclick="rollover(1)" src="images/backgroundfragments_03.jpg" alt="Translation games" >
我的问题是它仍然多次执行该功能。 我在这里做了一个jfiddle http://jsfiddle.net/RE3fp/,但是当我的Dreamweaver版本工作时,我无法使它工作。我的意思是我不能让“myfunction”div出现,而我的问题是它每次点击图像时都会执行。
答案 0 :(得分:2)