gpt4 book ai didi

javascript - 使用 JS 动态设置 z-index

转载 作者:行者123 更新时间:2023-11-28 15:54:20 25 4
gpt4 key购买 nike

我想在单击图标时更改 z-index - 每次用户单击图标时,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 );
});

最佳答案

差别很大

if( currentIndex = "auto" ) {

if( currentIndex == "auto") {

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

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

  1. 它尝试仅在赋值后增加值,这无论如何都会给你一个不正确的值,但是......
  2. 在你的例子中,“num”实际上是一个字符串,因为它是如何获取的。您需要将其转换为数字才能进行加法:parseInt(num) + 1

关于javascript - 使用 JS 动态设置 z-index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19416720/

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