简体   繁体   English

使用动态jQuery选择器

[英]Using dynamic jQuery selectors

I'm fairly new to jQuery and want to move past using explicit selectors and muddying up my code this way. 我对jQuery相当陌生,想使用显式选择器跳过过去,以这种方式弄乱我的代码。 I have tried a few different things but am unsuccessful at getting my selectors to dynamically function on multiple elements without having a snippet of code for each element. 我尝试了几种不同的方法,但是没有使我的选择器在多个元素上动态运行而没有每个元素的代码段的情况下,未能成功。

My script is simply as follows: 我的脚本如下:

     <script type="text/javascript">
        $(document).ready(function() {


            $("#navItem").mouseenter(function(){

                $(this).animate({
                    height: "150px"
                }, 500, "easeOutBounce")
            })

            $("#navItem").mouseleave(function(){

                $(this).animate({
                    height: "120px"
                }, 500, "easeOutBounce")
            })
    </script>

And my HTML.. 还有我的HTML

<div class="navWrap">

     <div id="navItem" class="navButton blue"></div>
     <div id="navItem2" class="navButton orange"></div>
     <div id="navItem3" class="navButton green"></div>
     <div id="navItem4" class="navButton red"></div>


</div>

I left out the remainder of the script because it's repetitive (the same functions for the rest of the IDs you see in the HTML). 我省略了脚本的其余部分,因为它是重复性的(与您在HTML中看到的其他ID的功能相同)。 My goal is to be able to dynamically select the "current" element that is being hovered over, rather than explicitly referencing each ID. 我的目标是能够动态选择要悬停的“当前”元素,而不是显式引用每个ID。 I assume I need to use the "this" selector, but the documentation I've found I have trouble relating back to my scenario. 我假设我需要使用“ this”选择器,但是我发现的文档在与我的方案有关时遇到了麻烦。

Any help is appreciated, thanks all! 感谢您的任何帮助,谢谢大家!

$('.navButton').hover(function(){
         $(this).animate({
                height: "150px"
            }, 500, "easeOutBounce");
},function(){
          $(this).animate({
                height: "120px"
            }, 500, "easeOutBounce")
});

selectors in jquery are basically the same as css selectors, so selecting by class will make a jquery object containing all elements with that class. jquery中的选择器与css选择器基本相同,因此按类选择将使jquery对象包含该类的所有元素。 http://api.jquery.com/category/selectors/ http://api.jquery.com/category/selectors/

When you apply a jquery function typically $(this) refers to a single specific element in question rather than the entire list, so use $(this) to effect elements selected by class will work fine. 当您应用jquery函数时,通常$(this)指的是所讨论的单个特定元素,而不是整个列表,因此使用$(this)来影响类选择的元素将可以正常工作。 I you need multiple bindings to a group you should check out .each http://api.jquery.com/each/ 我需要将多个绑定绑定到一个组,因此您应该检出.each http://api.jquery.com/each/

I am using .hover here which is a shorthand for mouseenter and mouseleave http://api.jquery.com/hover/ 我在这里使用.hover,这是mouseenter和mouseleave的简写http://api.jquery.com/hover/

Try something like this : 尝试这样的事情:

<script type = "text/javascript" > 
    $(document).ready(function() {
        $('.navButton').on('mouseenter mouseleave', function(e) {
            $(this).animate({
                height: e.type == "mouseenter" ? 150 : 120
            }, 500, "easeOutBounce")
        });
    });
</script>​​​​​​​​​​​​​​

Try this code: 试试这个代码:

 <script type="text/javascript">
        $(document).ready(function() {


            $("div[id^='navItem']").mouseenter(function(){

                $(this).animate({
                    height: "150px"
                }, 500, "easeOutBounce")
            })

            $("div[id^='navItem']").mouseleave(function(){

                $(this).animate({
                    height: "120px"
                }, 500, "easeOutBounce")
            })
    </script>

Change your script like this: 像这样更改脚本:

 <script type="text/javascript">
    $(document).ready(function() {
        var $navItems = $('.navitem');

        $navitems.each( function(){
            $(this).mouseenter(function(){

                $(this).animate({
                    height: "150px"
                }, 500, "easeOutBounce")
            })

            $(this).mouseleave(function(){

                $(this).animate({
                    height: "120px"
                }, 500, "easeOutBounce")
        });
    })
</script>

and html like this 和html这样

<div class="navWrap">

     <div id="navItem" class="navButton blue"></div>
     <div id="navItem" class="navButton orange"></div>
     <div id="navItem" class="navButton green"></div>
     <div id="navItem" class="navButton red"></div>


</div>

Since .hover() is shorthand for mouseenter and mouseleave , you could condense the code to: 由于.hover()是简写mouseentermouseleave ,你可以凝聚的代码:

$(".navWrap .navButton").hover(function() {
    $(this).animate({
        height: "150px"
    }, 500, "easeOutBounce")
}, function() {
    $(this).animate({
        height: "120px"
    }, 500, "easeOutBounce")
})​;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM