你好,
这是我的问题。我有这个 Javascript,当我输入
under[i].addEventListener("click", function(){
alert(position.y + " " + position.x);
score[i].setAttribute("style","-webkit-transform: translateY(-" + position.y + "px); -webkit-transition: .5s ease-in-out .01s;");
under[i].classList.add("popout");
score[i].classList.add("popScore");
under[i].classList.remove("underlayer_score");
score[i].classList.remove("scores_teams");
});
这很奇怪,因为当我在此事件侦听器之前或之后检查时,值仍然分配在该变量中。我仍然是 Javascript 的初学者,所以如果我的解释不是很清楚,请见谅。
谢谢您的帮助。
这是Javascript:
<script>
var under = new Array(10);
score = new Array(10);
var position = 0;
for(var i = 0; i <= 2; i++){
under[i] = document.getElementsByClassName('underlayer_score')[i];
score[i] = document.getElementsByClassName('scores_teams')[i];
}
function getPosition(element) {
var xPosition = 0;
var yPosition = 0;
while(element) {
xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
element = element.offsetParent;
}
return { x: xPosition, y: yPosition };
}
for(var i = 0; i <= 2; i++){
position = getPosition(score[i]);
under[i].addEventListener("click", function(){
alert(position.y + " " + position.x);
score[i].setAttribute("style","-webkit-transform: translateY(-" + position.y + "px); -webkit-transition: .5s ease-in-out .01s;");
under[i].classList.add("popout");
score[i].classList.add("popScore");
under[i].classList.remove("underlayer_score");
score[i].classList.remove("scores_teams");
});
alert(position.y);
position.x = 0;
position.y = 0;
}
</script>