Javascript 用JS动态设置z索引
我想在单击图标时更改z-index-每次用户单击图标时,索引z为+1,但我的代码不起作用: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
$(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++”,原因有二
parseInt(num)+1
两者之间有很大的区别
if( currentIndex = "auto" ) {
及
第一个执行您不希望的赋值,总是返回“auto”作为结果,if语句总是运行,将currentIndex重置为0
404也是正确的,您不想在这种情况下使用“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代码>增量+赋值