我正在使用jQuery toggle()事件在切换输入框时更改某些样式。我的项目是在Magento 2中开发的。但是该功能无法正常工作。该事件是在第一次点击时触发的,但是第二次点击没有任何反应。第三次单击将再次触发该事件。因此,问题在于事件仅在替代点击中触发。
<script>
require([
'jquery',
'accordion'
], function($) {
jQuery('.control').toggle(function() {
console.log(1);
jQuery('.hfb-header').css('margin-bottom', '80px');
}, function() {
console.log(2);
jQuery('.hfb-header').css('margin-bottom', '20px');
});
});
</script>
预期结果:
第一次单击:类hfb-header的margin-bottom应该更改为80px
第二次单击:类hfb-header的margin-bottom应该更改为20px
第3次点击:类hfb-header的margin-bottom应该更改为80px
实际结果:
第一次单击:类hfb-header的margin-bottom更改为80px
第二次点击:不变
第3次点击:类hfb-header的下边距已更改为20px
尝试此script
<script>
$(document).ready(function(){
$(".control").toggle(
function() {$(".hfb-header").css({"margin-bottom" : "80px"});},
function() {$(".hfb-header").css({"margin-bottom" : "20px"});
})
});
</script>
这只是一个简单有效的基本<script>
!