Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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 用JS动态设置z索引_Javascript_Jquery_Dom Manipulation - Fatal编程技术网

Javascript 用JS动态设置z索引

Javascript 用JS动态设置z索引,javascript,jquery,dom-manipulation,Javascript,Jquery,Dom Manipulation,我想在单击图标时更改z-index-每次用户单击图标时,索引z为+1,但我的代码不起作用: $(document).on('click', '.icon-layer-up', function() { console.log($(this).parent(".ui-wrapper").css("z-index")); var currentIndex = $(this).parent(".ui-wrapper").css("z-index"); if

我想在单击图标时更改z-index-每次用户单击图标时,索引z为+1,但我的代码不起作用:

  $(document).on('click', '.icon-layer-up', function() { 
      console.log($(this).parent(".ui-wrapper").css("z-index"));
      var currentIndex = $(this).parent(".ui-wrapper").css("z-index"); 
      if ( currentIndex = "auto" ) {
        currentIndex = 0;
      }
      var num = currentIndex++;
       $(this).parent(".ui-wrapper").css("z-index", num );
    });

您的问题是
var num=currentIndex++

currentIndex++
currentIndex
增加到
currentIndex+1
,但它将返回原始值,因此
num
被分配到
currentIndex
的原始值。只需使用var num=currentIndex+1


如果您只想添加1,那么使用
++
并不是很好的编码实践。如果只是添加,请使用
+1

您的问题是
var num=currentIndex++

currentIndex++
currentIndex
增加到
currentIndex+1
,但它将返回原始值,因此
num
被分配到
currentIndex
的原始值。只需使用var num=currentIndex+1


如果您只想添加1,那么使用
++
并不是很好的编码实践。如果只是添加,请使用
+1

这两种方法之间有很大的区别

if( currentIndex = "auto" ) {

第一个执行您不希望的赋值,总是返回“auto”作为结果,if语句总是运行,将currentIndex重置为0

404也是正确的,您不想在这种情况下使用“num++”,原因有二

  • 它试图仅在赋值后增加值,这会给您一个错误的值,但是
  • 在您的例子中,“num”实际上是一个字符串,因为它是如何获得的。您需要将其转换为数字才能进行加法:
    parseInt(num)+1

  • 两者之间有很大的区别

    if( currentIndex = "auto" ) {
    

    第一个执行您不希望的赋值,总是返回“auto”作为结果,if语句总是运行,将currentIndex重置为0

    404也是正确的,您不想在这种情况下使用“num++”,原因有二

  • 它试图仅在赋值后增加值,这会给您一个错误的值,但是
  • 在您的例子中,“num”实际上是一个字符串,因为它是如何获得的。您需要将其转换为数字才能进行加法:
    parseInt(num)+1

  • 我在代码中注意到的第一件事(可能是问题,也可能不是问题)是缺少jQuery
    on
    事件的数据参数。您还需要将事件应用于
    document.body
    ,而不是
    document

    $(document.body/*changed*/).on('click', '.icon-layer-up', {}/*added*/, function() {
    
    接下来,您总是将
    currentIndex
    设置为
    auto
    ,然后设置为
    0
    ,而不是检查它是否等于
    auto

    if ( currentIndex ==/*fixed*/ "auto" ) {
    
    此外,您最初将
    currentIndex
    设置为一个字符串,在尝试按您现在的方式递增字符串时,它只会将字符串转换为一个数字。您必须首先尝试将其转换为
    编号
    ,检查以确保它是
    编号
    ,然后将其递增

    因此,总的来说,固定代码应该是:

      $(document.body).on('click', '.icon-layer-up', {}, function() { 
          console.log($(this).parent(".ui-wrapper").css("z-index"));
          var currentIndex = Number($(this).parent(".ui-wrapper").css("z-index")); 
          if ( isNaN(currentIndex) ) { // if is not a number, set it to 0
            currentIndex = 0;
          }
          var num = currentIndex++;
           $(this).parent(".ui-wrapper").css("z-index", num );
        });
    
    接下来,确保您阅读了有关z-index的
    z-index
    ,并了解其工作原理<代码>z索引不会应用于
    静态
    的默认
    位置
    的元素。尝试将元素设置为
    位置:相对,您正试图对其应用
    z-index

    z-index的参考资料



    我注意到您的代码中的第一件事,这可能是问题,也可能不是问题,就是您缺少jQuery
    上的
    事件的数据参数。您还需要将事件应用于
    document.body
    ,而不是
    document

    $(document.body/*changed*/).on('click', '.icon-layer-up', {}/*added*/, function() {
    
    接下来,您总是将
    currentIndex
    设置为
    auto
    ,然后设置为
    0
    ,而不是检查它是否等于
    auto

    if ( currentIndex ==/*fixed*/ "auto" ) {
    
    此外,您最初将
    currentIndex
    设置为一个字符串,在尝试按您现在的方式递增字符串时,它只会将字符串转换为一个数字。您必须首先尝试将其转换为
    编号
    ,检查以确保它是
    编号
    ,然后将其递增

    因此,总的来说,固定代码应该是:

      $(document.body).on('click', '.icon-layer-up', {}, function() { 
          console.log($(this).parent(".ui-wrapper").css("z-index"));
          var currentIndex = Number($(this).parent(".ui-wrapper").css("z-index")); 
          if ( isNaN(currentIndex) ) { // if is not a number, set it to 0
            currentIndex = 0;
          }
          var num = currentIndex++;
           $(this).parent(".ui-wrapper").css("z-index", num );
        });
    
    接下来,确保您阅读了有关z-index的
    z-index
    ,并了解其工作原理<代码>z索引
    不会应用于
    静态
    的默认
    位置
    的元素。尝试将元素设置为
    位置:相对,您正试图对其应用
    z-index

    z-index的参考资料



    在这种情况下,使用
    var num=++currentIndex增量+赋值在这种情况下使用
    var num=++currentIndex增量+赋值