Javascript jQuery/JS-变量不';似乎没有更新?
我希望用户单击一个DIV元素,让它在JavaScript中向一个变量添加1,并在该变量的值更改时发生不同的事情。当我第一次单击div元素时,它会显示“You's here!”,但当我第二次单击div元素时……似乎什么都没有发生。看起来变量没有被更新Javascript jQuery/JS-变量不';似乎没有更新?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我希望用户单击一个DIV元素,让它在JavaScript中向一个变量添加1,并在该变量的值更改时发生不同的事情。当我第一次单击div元素时,它会显示“You's here!”,但当我第二次单击div元素时……似乎什么都没有发生。看起来变量没有被更新 var $test = 0; $( document ).ready(function() { if ($test == 0) { $("#textbox").click(function() { $(textbox).te
var $test = 0;
$( document ).ready(function() {
if ($test == 0) {
$("#textbox").click(function() {
$(textbox).text("You and here!");
$test = $test + 1;
});
};
if ($test == 1) {
$("#textbox").click(function() {
$(textbox).text("Now, you are there!");
$test = $test + 1;
});
};
});
请用下面的方式编写代码,希望对您有所帮助
var $test = 0;
$(document).ready(function () {
$("#textbox").click(function () {
if ($test == 0) {
$(textbox).text("You and here!");
};
if ($test == 1) {
$(textbox).text("Now, you are there!");
};
$test = $test + 1;
});
});
请用下面的方式编写代码,希望对您有所帮助
var $test = 0;
$(document).ready(function () {
$("#textbox").click(function () {
if ($test == 0) {
$(textbox).text("You and here!");
};
if ($test == 1) {
$(textbox).text("Now, you are there!");
};
$test = $test + 1;
});
});
您需要检查单击处理程序中的条件
$(document).ready(function () {
//declare it as a closure variable instead of addint it to the global scope
var test = 0;
$("#textbox").click(function () {
//check the value of test here
if (test == 0) {
$(textbox).text("You and here!");
} else if (test == 1) { //or just else
$(textbox).text("Now, you are there!");
}
//increment the value of test
test++;
});
});
您需要检查单击处理程序中的条件
$(document).ready(function () {
//declare it as a closure variable instead of addint it to the global scope
var test = 0;
$("#textbox").click(function () {
//check the value of test here
if (test == 0) {
$(textbox).text("You and here!");
} else if (test == 1) { //or just else
$(textbox).text("Now, you are there!");
}
//increment the value of test
test++;
});
});
Test th$Test变量位于事件处理程序内部,在文档就绪事件中只需要一个事件hanler attach。
顺便说一句,document ready函数只运行一次。在事件处理程序中测试th$Test变量,在document ready事件中只需要一个事件hanler attach。
顺便说一句,document ready函数只运行一次。错误在于:您正在为
$test
变量的每个值注册一个新的事件处理程序。相反,请尝试以下方法:
var $test = 0;
$( document ).ready(function() {
$("#textbox").click(function() {
switch($test) {
case 0:
$(this).val("You and here!");
$test = $test + 1;
break;
case 1:
$(this).val("Now, you are there!");
$test = $test + 1;
break;
}
});
});
错误在于:您正在为
$test
变量的每个值注册一个新的事件处理程序。相反,请尝试以下方法:
var $test = 0;
$( document ).ready(function() {
$("#textbox").click(function() {
switch($test) {
case 0:
$(this).val("You and here!");
$test = $test + 1;
break;
case 1:
$(this).val("Now, you are there!");
$test = $test + 1;
break;
}
});
});
仅我的版本:
$(document).ready(function () {
var test = 0,
$textBox = $('#textbox');
$textbox.on('click', function () {
var message;
if (test == 0) { message = "You and here!"; }
if (test == 1) { message = "Now, you are there!"; }
if (message) { $textbox.text(message); }
test++;
});
});
咖啡只是为了好玩
$ ->
test = 0
$textBox = $ '#textbox'
$textBox.on 'click', ->
message = switch test
when 0 then "You and here!"
when 1 then "Now, you are there!"
$textbox.text message if message
test++
只是我的版本:
$(document).ready(function () {
var test = 0,
$textBox = $('#textbox');
$textbox.on('click', function () {
var message;
if (test == 0) { message = "You and here!"; }
if (test == 1) { message = "Now, you are there!"; }
if (message) { $textbox.text(message); }
test++;
});
});
咖啡只是为了好玩
$ ->
test = 0
$textBox = $ '#textbox'
$textBox.on 'click', ->
message = switch test
when 0 then "You and here!"
when 1 then "Now, you are there!"
$textbox.text message if message
test++
@C-link不知道OP在测试变为2或更多时想要什么:(可能是他愿意
test=test-1;
如果test==1
@C-link不知道OP在测试变为2或更多时想要什么:(可能他愿意test=test-1;
以防万一test==1
你确定吗?看看小提琴吧。$test++有什么神奇的功能,$test=$test+1却做不到?!什么都没有。这只是一种不寻常的写增量的方式,我没有看到。对不起,不需要道歉。有近20年的编码经验,我唯一能做的事情假设是-不同的编码风格可以实现相同的最终结果。不要在不花一分钟时间思考代码在做什么的情况下发表评论。你确定吗?看看小提琴。$test++能做什么?$test=$test+1不能做什么?!没什么。这只是一种不寻常的编写增量的方式,我没有看到。对不起,不需要道歉。Wit将近20年的编码经验,我唯一能说的是——不同的编码风格可以达到相同的最终结果。不要不花一分钟去思考代码在做什么就发表评论。