简化多个元素的 jQuery 函数

发布于 2024-12-04 16:07:45 字数 1746 浏览 4 评论 0原文

我正在研究我的新产品组合,并且有一个使用showhide函数的项目列表。就我而言,我为每个项目编写代码,但这是非常重复的。因此,我想知道是否有一种为每个项目编写相同功能的技术。

这是我的jQuery代码:

$project1.hide();
$("a.show_hide_project1").show();
$('a.show_hide_project1').click(function() {
    $project1.delay(100).slideToggle("slow");
    $project2.hide(); $project3.hide(); $project4.hide(); $project5.hide();
    $project6.hide(); $project7.hide(); $project8.hide(); $project9.hide();
    $project10.hide();$project11.hide(); $project12.hide();
});

$('a.next1').click(function() {
    $project2.fadeToggle("slow");
    $project1.delay(600).hide();
});

$project2.hide();
$("a.show_hide_project2").show();
$('a.show_hide_project2').click(function() {
    $project2.delay(100).slideFadeToggle("slow");
    $project1.hide(); $project3.hide(); $project4.hide(); $project5.hide();
    $project6.hide(); $project7.hide(); $project8.hide(); $project9.hide();
    $project10.hide();$project11.hide(); $project12.hide();
});

$('a.next2').click(function() {
    $project3.fadeToggle("slow");
    $project2.hide();
});
$('a.previous2').click(function(){
$project1.fadeToggle();
$project2.hide();
});

$project3.hide();
$("a.show_hide_project3").show();
$('a.show_hide_project3').click(function() {
    $project3.delay(100).slideFadeToggle("slow");
    $project2.hide(); $project1.hide(); $project4.hide(); $project5.hide();
    $project6.hide(); $project7.hide(); $project8.hide(); $project9.hide();
    $project10.hide();$project11.hide(); $project12.hide();
});

$('a.next3').click(function(){
    $project4.fadeToggle("slow");
    $project3.hide();
});
$('a.previous3').click(function() {
    $project2.fadeToggle();
    $project3.hide();
});

任何帮助将不胜感激。 提前致谢。

I'm working on my new portfolio and I have a list of projects which are using show and hide functions. In my case, I'wrote the code for each project, but is it very repetitive. So I would like to know if there's a technique to write the same function for each project.

Here is my jquery code:

$project1.hide();
$("a.show_hide_project1").show();
$('a.show_hide_project1').click(function() {
    $project1.delay(100).slideToggle("slow");
    $project2.hide(); $project3.hide(); $project4.hide(); $project5.hide();
    $project6.hide(); $project7.hide(); $project8.hide(); $project9.hide();
    $project10.hide();$project11.hide(); $project12.hide();
});

$('a.next1').click(function() {
    $project2.fadeToggle("slow");
    $project1.delay(600).hide();
});

$project2.hide();
$("a.show_hide_project2").show();
$('a.show_hide_project2').click(function() {
    $project2.delay(100).slideFadeToggle("slow");
    $project1.hide(); $project3.hide(); $project4.hide(); $project5.hide();
    $project6.hide(); $project7.hide(); $project8.hide(); $project9.hide();
    $project10.hide();$project11.hide(); $project12.hide();
});

$('a.next2').click(function() {
    $project3.fadeToggle("slow");
    $project2.hide();
});
$('a.previous2').click(function(){
$project1.fadeToggle();
$project2.hide();
});

$project3.hide();
$("a.show_hide_project3").show();
$('a.show_hide_project3').click(function() {
    $project3.delay(100).slideFadeToggle("slow");
    $project2.hide(); $project1.hide(); $project4.hide(); $project5.hide();
    $project6.hide(); $project7.hide(); $project8.hide(); $project9.hide();
    $project10.hide();$project11.hide(); $project12.hide();
});

$('a.next3').click(function(){
    $project4.fadeToggle("slow");
    $project3.hide();
});
$('a.previous3').click(function() {
    $project2.fadeToggle();
    $project3.hide();
});

Any help will be very appreciated.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆悲凉 2024-12-11 16:07:45

这是一个快速介绍: http://jsfiddle.net/Jj2Q7/1/

动画是不完全像你所拥有的那样,我怀疑这是否是最有效的方法,但希望它能作为一个起点。

更新

这是另一个自由使用数据属性的版本: http://jsfiddle.net/Jj2Q7/3/

它应该比以前更快,但依赖于具有正确属性集的 HTML 标记。

Here's a quick stab at it: http://jsfiddle.net/Jj2Q7/1/

The animations are not exactly as you have it, and I doubt if that's the most efficient way to do it, but hopefully it'll serve as a starting point.

Update

Here's another version which makes liberal use of data attributes: http://jsfiddle.net/Jj2Q7/3/

It should be faster than the previous, but relies on the HTML tags having the right attributes set.

惜醉颜 2024-12-11 16:07:45

首先,您可能应该利用一个通用的 css 类来选择所有这些

$('.projectItem').click(function () {
    $('.projectItem').hide(); //hide them all
    $(this).delay(100).slideFadeToggle("slow"); //then show the one being clicked.
 });

,您的 Html 看起来更像这样。

<div class="projectItem" id="project1"/>
<div class="projectItem" id="project2"/>
<div class="projectItem" id="project3"/>
<div class="projectItem" id="project4"/>

只是一个非常粗略的想法。

First off, you should probably be leveraging a common css class to select all of them

$('.projectItem').click(function () {
    $('.projectItem').hide(); //hide them all
    $(this).delay(100).slideFadeToggle("slow"); //then show the one being clicked.
 });

Where your Html would look more like this.

<div class="projectItem" id="project1"/>
<div class="projectItem" id="project2"/>
<div class="projectItem" id="project3"/>
<div class="projectItem" id="project4"/>

Just a really rough idea.

泛泛之交 2024-12-11 16:07:45

基本上,您需要为您的 HTML 提供一种类,使其可识别为“项目”,并向各个项目添加一些 rel 或 data 属性
然后您的代码将转换为如下内容:(取决于您的 HTML,可能会有所不同)

$(function(){
    $(".project").hide();
    $("a.show_hide_project").show();

    $('a.show_hide_project').click(function(){
         $(".project").hide();
         $(".project[rel="+$(this).attr('rel')+"]").delay(100).slideFadeToggle("slow");

    });  
});

Basically you need to give your HTML a sort of class that makes it identifiable as a "project" , and add some rel or data attribute to the individual projects
Your code then translates to something like this: (depending on your HTML , may vary)

$(function(){
    $(".project").hide();
    $("a.show_hide_project").show();

    $('a.show_hide_project').click(function(){
         $(".project").hide();
         $(".project[rel="+$(this).attr('rel')+"]").delay(100).slideFadeToggle("slow");

    });  
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文