gpt4 book ai didi

Javascript 事件处理程序不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:42:04 26 4
gpt4 key购买 nike

我试图在单击按钮时增加/减少段落中的值。

$(document).ready(function() {
var breakTime = 5;
var section = 25;
var start = "Start";
var stop = "Stop";


function Pomodoro(element, target) {
this.element = element;
this.target = target;
};

Pomodoro.prototype.incrementer = function incrementer() {
// this takes care of break and section timers incrementing

this.element.click(function() {
breakTime++;
var el = this.target;
el.html(breakTime);
});
};
// end

Pomodoro.prototype.decrementer = function decrementer() {
// this takes care of break and section timers incrementing

breakerDec.element.click(function() {
breakTime--;
var el = breakerDec.target.html(breakTime);
});
};
// end

var breakerInc = new Pomodoro();
var ele = $("#inner1");
var tar = $("#par");

breakerInc.element = ele;
breakerInc.target = tar;

breakerInc.incrementer.bind(breakerInc);
//end

var breakerDec = new Pomodoro();

breakerDec.element = $("#inner2");
breakerDec.target = $("#par");

breakerDec.decrementer();

var sectionInc = new Pomodoro($("#inner3"), $("#par2"));
sectionInc.incrementer.bind(sectionInc);
});

当我点击按钮时我没有得到任何结果。这是 html:

<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pomodoro Timer</title><!-- End of Title -->
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="wrapper">
<div class="top">
<div class="div div-1"><p id="par" class="breaktime-1">5</p>
<div class="inner-div inner-1"><button id="inner1" type="button" class="btn">+</button></div>
<div class="inner-div inner-2"><button id="inner2" type="button" class="btn">-</button></div>
</div>
<div class="div div-2"><p id="par2" class="breaktime">25</p>
<div class="inner-div inner-1"><button id="inner3" type="button" class="btn">+</button></div>
<div class="inner-div inner-2"><button id="inner4" type="button" class="btn">-</button></div>
</div>
</div>
<div class="div div-3">
<h3 class="heading">section</h3>
<button type="button" class="btn-Start-Stop"></button
</div>
<div><p></p></div>
</div>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.js"></script>
<script type="text/javascript" src="main.js"></script>
</body>
</html>

最佳答案

这是一个(简化的)解决方案,仅使用 vanilla javascript:

function changeValue() {
var breaktime = this.parentNode.getElementsByClassName('breaktime')[0];
var val = breaktime.innerHTML;

if (this.className === 'increment') {
val++;
}

else if (this.className === 'decrement') {
val--;
}

breaktime.innerHTML = val;
}


function startStop() {
var values = [];
var breaktimes = document.getElementsByClassName('breaktime');

breaktimes[1].classList.toggle('decrementing');

if (breaktimes[1].classList.contains('decrementing') === true) {

values[1] = parseInt(breaktimes[1].innerHTML);

values[1]--;
breaktimes[1].innerHTML = values[1];

var decrementer = setInterval(function(){

values[0] = parseInt(breaktimes[0].innerHTML);
values[1] = parseInt(breaktimes[1].innerHTML);

if (breaktimes[1].classList.contains('decrementing') !== true) {
clearInterval(decrementer);
}

values[1]--;
breaktimes[1].innerHTML = values[1];

if (values[1] === values[0]) {
window.alert('The counter has reached ' + values[1]);
clearInterval(decrementer);
}

}, 1000);

}

}



var buttons = document.getElementsByTagName('button');
var startStopButton = buttons[(buttons.length -1)];

for (var i = 0; (i+1) < buttons.length; i++) {
buttons[i].addEventListener('click',changeValue,false);
}

startStopButton.addEventListener('click',startStop,false);
<section>

<div>
<p class="breaktime">5</p>
<button id="inner1" type="button" class="increment">+</button>
<button id="inner2" type="button" class="decrement">-</button>
</div>

<div>
<p class="breaktime">25</p>
<button id="inner3" type="button" class="increment">+</button>
<button id="inner4" type="button" class="decrement">-</button>
</div>

<div>
<h3>Section</h3>
<button type="button" class="btn-Start-Stop">Start / Stop</button>
</div>

<div>
<p></p>
</div>

</section>

关于Javascript 事件处理程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34920833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com