Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery变量添加问题_Javascript_Jquery_Html - Fatal编程技术网

Javascript jquery变量添加问题

Javascript jquery变量添加问题,javascript,jquery,html,Javascript,Jquery,Html,我的变量添加到自身时遇到问题 变量以0开头,然后将1添加到自身中,然后再也不执行此操作。 我想知道是否有人知道如何解决这个问题,因为我的主要问题是变量不能是全局变量,因为将使用多个变量。该表为12x24,这意味着存在大量的td 例如: 如果单击td:n子项(1),则将使用不同的变量 与td相比:第n个孩子(5) 以下是我的代码(): jQuery: $('table.masteries tr.p0 td').on('click', function(){ i = 0 s = $(

我的变量添加到自身时遇到问题

变量以
0
开头,然后将
1
添加到自身中,然后再也不执行此操作。 我想知道是否有人知道如何解决这个问题,因为我的主要问题是变量不能是全局变量,因为将使用多个变量。该表为12x24,这意味着存在大量的
td

例如:

如果单击td:n子项(1),则将使用不同的变量 与td相比:第n个孩子(5)

以下是我的代码():

jQuery:

$('table.masteries tr.p0 td').on('click', function(){
    i = 0
    s = $(this).find('p').text()
    current_max = parseInt(s.substr(s.length - 1))
    if (
        $(this).is($(':nth-child(1)'))
        || $(this).is($(':nth-child(2)'))
        || $(this).is($(':nth-child(3)'))
        || $(this).is($(':nth-child(4)'))
    ) {
        i = i + 1 /<-- the issue
        console.log(i) /<-- the issue
        $(this).closest('span').append(i); /<-- the issue
    } else if (
        $(this).is($(':nth-child(5)'))
        || $(this).is($(':nth-child(6)'))
        || $(this).is($(':nth-child(7)'))
        || $(this).is($(':nth-child(8)'))
    ) {

    } else if (
        $(this).is($(':nth-child(9)'))
        || $(this).is($(':nth-child(10)'))
        || $(this).is($(':nth-child(11)'))
        || $(this).is($(':nth-child(12)'))
    ) {

    }
});
$('table.masteries tr.p0 td')。在('click',function()上{
i=0
s=$(this.find('p').text()
当前_max=parseInt(s.substr(s.length-1))
如果(
$(此).is($(':第n个子项(1)'))
||$(此).is($(':第n个子项(2)'))
||$(此).is($(':第n个孩子(3)'))
||$(此).is($(':第n个孩子(4)'))
) {
i=i+1/变化

i = 0

因此,如果已经设置了
i
,则不会将其重置为0

更新

您还需要将
i
的所有实例更改为
this.i
,因为
i
是一个局部函数变量,每次回调函数关闭时都会丢失该变量


每次单击时,您都会将“i”重置为=0,并且只增加1。因此,“i”永远不能大于1。请删除所有的
is(“:n个子项”)
,只需使用
的索引即可。这将使代码更简单,更易于阅读。您可以使用
+=
向自己添加内容。
i+=5
i=i+5
相同嘿!我试过这么做()我刚刚收到一个控制台错误,说我没有设置。@SaiyanToaster我更新了我的答案,因为
I
是一个局部函数变量
i = 0
if(!i){
   i = 0;
}