我对css()函数有一个奇怪的问题.这是我的代码片段.
console.log('' + $(this).css('left') + ':' + $(this).css('top'));
console.log('Desired position - ' + return_to_left + ':' + return_to_top);
$(this).css('top', return_to_top + 'px');
$(this).css('left', return_to_left + 'px');
console.log('Finally: ' + $(this).css('left') + ':' + $(this).css('top'));
Run Code Online (Sandbox Code Playgroud)
我在控制台上得到的输出就是这个.
458px:2113px
Desired position - 448px:2102px;
Finally: 458px:2113px;
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议为什么会发生这种情况?我尝试过'!important'.没有帮助.
(另外,对于上下文,此代码是动画后回调函数的一部分.它尝试将元素放回到动画开始之前的位置.)
谢谢您的帮助.
发现问题!!
这个:
console.log('Desired position - ' + return_to_left + ':' + return_to_top);
Run Code Online (Sandbox Code Playgroud)
输出...
Desired position - 448px:2102px;
Run Code Online (Sandbox Code Playgroud)
因此你的变量return_to_left和return_to_topALREADY最后都有px字符串.
你的css调用如下:
$(this).css('top', '448pxpx');
$(this).css('left', '2102pxpx');
Run Code Online (Sandbox Code Playgroud)
哪个不行!:p + 'px'从css调用的末尾删除.