jQuery代码太长,需要帮助简化[关闭]

osh3o9ms  于 2023-11-17  发布在  jQuery
关注(0)|答案(2)|浏览(139)

已关闭。此问题需要更多focused。目前不接受回答。
**要改进此问题吗?**更新此问题,使其仅针对editing this post的一个问题。

29天前关闭
Improve this question
这段代码是工作,但我希望得到这个简化。我不知道如何使这个工作在一个非常简单的方式。期待大家的帮助。任何想法都欢迎。

  1. jQuery("#state-of-the-art .fl-tabs-label").click(function() {
  2. stateOftheArt();
  3. });
  4. jQuery("#everything-you-need .fl-tabs-label").click(function() {
  5. everythingYouNeed();
  6. });
  7. jQuery("#monitor-your-sites .fl-tabs-label").click(function() {
  8. monitorYourSites();
  9. });
  10. function stateOftheArt() {
  11. jQuery("#state-of-the-art .fl-tabs-panel").addClass("tab-animation-fadeup");
  12. setTimeout(function () {
  13. jQuery("#state-of-the-art .fl-tabs-panel").removeClass("tab-animation-fadeup");
  14. }, 400);
  15. return
  16. }
  17. function everythingYouNeed() {
  18. jQuery("#everything-you-need .fl-tabs-panel").addClass("tab-animation-fadeup");
  19. setTimeout(function () {
  20. jQuery("#everything-you-need .fl-tabs-panel").removeClass("tab-animation-fadeup");
  21. }, 400);
  22. return
  23. }
  24. function monitorYourSites() {
  25. jQuery("#monitor-your-sites .fl-tabs-panel").addClass("tab-animation-fadeup");
  26. setTimeout(function () {
  27. jQuery("#monitor-your-sites .fl-tabs-panel").removeClass("tab-animation-fadeup");
  28. }, 400);
  29. return
  30. }

字符串

gopyfrb3

gopyfrb31#

没有你的html代码,我只能猜测,但这可能是一个解决方案:

  1. function tabAnimation(obj) {
  2. var _obj = jQuery(obj);
  3. _obj.addClass("tab-animation-fadeup");
  4. setTimeout(function () {
  5. _obj.removeClass("tab-animation-fadeup");
  6. }, 400);
  7. }
  8. jQuery(".fl-tabs-label").click(function () {
  9. tabAnimation(this);
  10. });

字符串
您可以在click函数中移动tabAnimation的内容。

Demo

  1. function tabAnimation(obj) {
  2. var _obj = jQuery(obj);
  3. _obj.addClass("tab-animation-fadeup");
  4. setTimeout(function () {
  5. _obj.removeClass("tab-animation-fadeup");
  6. }, 400);
  7. }
  8. jQuery(".fl-tabs-label").click(function () {
  9. tabAnimation(this);
  10. });

x

  1. .tab-animation-fadeup{
  2. color:red;
  3. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <div class="fl-tabs-label">
  3. div1
  4. </div>
  5. <div class="fl-tabs-label">
  6. div2
  7. </div>
  8. <div class="fl-tabs-label">
  9. div3
  10. </div>

的字符串

展开查看全部
cqoc49vn

cqoc49vn2#

你可以创建一个函数,把你的html元素作为参数。

  1. function anyFunction(element) {
  2. jQuery(`${element} .fl-tabs-panel`).addClass("tab-animation-fadeup");
  3. setTimeout(function () {
  4. jQuery(`${element} .fl-tabs-panel").removeClass("tab-animation-fadeup`);
  5. }, 400);
  6. }
  7. //call that function
  8. jQuery("#state-of-the-art .fl-tabs-label").click(function() {
  9. anyFunction('#state-of-the-art')
  10. });
  11. jQuery("#everything-you-need .fl-tabs-label").click(function() {
  12. anyFunction('#everything-you-need')
  13. });
  14. jQuery("#monitor-your-sites .fl-tabs-label").click(function() {
  15. anyFunction('#monitor-your-sites')
  16. });

字符串

展开查看全部

相关问题