我的目标是创建一个按钮,您可以单击该按钮来启动脚本。我希望能够多次单击该按钮以多次执行该脚本,并使该按钮具有从我的"点击"变量。
<div class="game-object">
<script type="text/javascript">
var clicks = 0;
document.getElementById("push").addEventListener("click", updateClickCount);
function updateClickCount() {
var el = document.getElementById("clickCount");
el.innerHTML = clicks;
el.style.color = '#'+Math.floor(Math.random()*16777215).toString(16);
el.style.fontSize = '70px';
}
</script>
<script>
var clicks = 0;
window.setInterval(
function () {
clicks = clicks + 1;
document.getElementById("clicks").innerHTML = clicks;
}, 1000);
</script>
<button type="button" onClick="clicks++;updateClickCount();" id="push" style="width:400px;height:60px;"><font size="5" face="verdana" color="white">Click me for Cola!</font></button>
<div id="clickCount"></div>
我不知道在代码中放置按钮的位置。
的Seb
答案 0 :(得分:1)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="push" style="width:400px;height:60px;">
<font size="5" face="verdana" color="white">Click me for Cola!</font>
</button>
<div id="clickCount"></div>
<script type="text/javascript">
var clicks = 0;
var h = null;
$("#push").click( function(){
var el = document.getElementById("clickCount");
el.innerHTML = clicks;
el.style.color = '#'+Math.floor(Math.random()*16777215).toString(16);
el.style.fontSize = '70px';
if(!h){
h = window.setInterval(function () {
clicks = clicks + 1;
document.getElementById("clickCount").innerHTML = clicks;
}, 1000);
}
});
</script>
&#13;