gpt4 book ai didi

jquery - 基于 data-* 属性显示和隐藏元素

转载 作者:行者123 更新时间:2023-12-01 00:37:14 25 4
gpt4 key购买 nike

这看起来对于 JQuery 来说应该是微不足道的,但是这个函数隐藏了整个表单......有人能指出我正确的方向吗?

$('form')
.children()
.filter(function(){
return $(this).data('show', 'pro')
})
.show();
$('form')
.children()
.filter(function(){
return $(this).data('show', 'home')
})
.hide();

最佳答案

您正在将 2 个参数传递给 data method ,从而设置它而不是检索旧值。

改用这个:

$('form')
.children()
.filter(function(){
return $(this).data('show') === 'pro';
})
.show();
$('form')
.children()
.filter(function(){
return $(this).data('show') === 'home';
})
.hide();

您还可以缓存选择器以提高性能(或使用 end )。

关于jquery - 基于 data-* 属性显示和隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9036559/

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