jquery – 在所有div的点击上动态应用幻灯片切换效果

前端之家收集整理的这篇文章主要介绍了jquery – 在所有div的点击上动态应用幻灯片切换效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当点击特定的li元素 – 我想要能够打开它的相应的div元素.

我有如下代码

<ul class="no-padding pro-list">
    <li><a href="#" class="pro-1 pro">A</a>
        <div class="proc-description panel-1">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-2 pro">B</a>
        <div class="proc-description panel-2">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-3 pro">C</a>
        <div class="proc-description panel-3">
            <p>a</p>
            <p>b</p>
            <p>c</p>
        </div>
    </li>
    <li><a href="#" class="pro-4 pro">D</a>
        <div class="proc-description panel-4">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-5 pro">E</a>
        <div class="proc-description panel-5">
            Hello world! Lorem ipsum doner inut.
                        <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-6 pro">F</a>
        <div class="proc-description panel-6">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-7 pro">G</a>
        <div class="proc-description panel-7">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-8 pro">H</a>
        <div class="proc-description panel-8">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="proc-9 ">I</a></li>
    <li><a href="#" class="proc-10 ">J</a></li>
    <li><a href="#" class="proc-11 ">K</a></li>
</ul>

当我添加这个脚本时它工作正常:

<script>
    jQuery(document).ready(function ($) {
        jQuery(".pro-2").click(function () {
            jQuery(".panel-2").slideToggle("slow");
        });
    });
</script>

但是我想让它变得动态.我不想为每个类和div类创建这么多的函数,而是要创建一个可以切换所有li元素的函数.

解决方法

您应该将事件与普通类绑定,然后使用遍历方法来识别要处理的元素.

您可以使用已添加的pro类添加到锚和proc-description添加到div.

使用

jQuery(document).ready(function($){
    jQuery("a.pro").click(function(){
       jQuery(this).next(".proc-description").slideToggle("slow");
   return false; 
  });            
});
jQuery(document).ready(function($){
    jQuery("a.pro").click(function(){
       jQuery(this).next(".proc-description").slideToggle("slow");
      
   });            
});
.proc-description {display:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="no-padding pro-list">
    <li><a href="#" class="pro-1 pro">A</a>

        <div class="proc-description panel-1">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-2 pro">B</a>

        <div class="proc-description panel-2">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-3 pro">C</a>

        <div class="proc-description panel-3">
            <p>a</p>
            <p>b</p>
            <p>c</p>
        </div>
    </li>
    <li><a href="#" class="pro-4 pro">D</a>

        <div class="proc-description panel-4">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-5 pro">E</a>

        <div class="proc-description panel-5">Hello world! Lorem ipsum doner inut.
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-6 pro">F</a>

        <div class="proc-description panel-6">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-7 pro">G</a>

        <div class="proc-description panel-7">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-8 pro">H</a>

        <div class="proc-description panel-8">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="proc-9 ">I</a>
    </li>
    <li><a href="#" class="proc-10 ">J</a>
    </li>
    <li><a href="#" class="proc-11 ">K</a>
    </li>
</ul>
原文链接:https://www.f2er.com/jquery/176764.html

猜你在找的jQuery相关文章