简体   繁体   English

Jquery影响具有相同名称的所有元素

[英]Jquery affecting all elements with same name

This is the code in my html file: 这是我的html文件中的代码:

    <div id="centerBlock">
        <block>
            <site style="padding-top: 10px; margin-left: 20px">
                <img src="./images/site1.png" alt="some_text"/>
            </site>
            <textOnImage style="padding-top: 10px; margin-left: 20px">
                lolol
            </textOnImage>
        </block>

        <block>
            <site style="padding-top: 163px; margin-left: 20px">
                <img src="./images/site1.png" alt="some_text"/>
            </site>
            <textOnImage style="padding-top: 163px; margin-left: 20px">
                lolol
            </textOnImage>
        </block>

    </div>

And this is my javascript: 这是我的javascript:

$("block").mouseenter(function(){        
    $("site").fadeTo("slow", 0.25);
    $("textOnImage").animate({ 
        opacity: "1"
    }, 600 );
});

$("block").mouseleave(function(){         
    $("site").fadeTo("slow", 1);
    $("textOnImage").animate({ 
        opacity: "0"
    }, 600 );
 });

However, when I hover over one of the two block elements they both go down in opacity and when I move my mouse away from it than they both go back to the original state. 然而,当我将鼠标悬停在两个块元素中的一个上时,它们都会以不透明度向下移动,当我将鼠标移开时,它们都会返回到原始状态。 I've searched for hours, and I'm 100% sure I've been searching the incorrect terms. 我搜索了几个小时,我100%肯定我一直在搜索错误的条款。 How do I do them individually? 我该如何单独进行?

Use this to target only the block that triggered the event and then use .find() to find the desired element in that specific block. 使用this仅定位触发事件的块,然后使用.find()在该特定块中查找所需的元素。 The way you were doing it before was obviously finding all site elements and all textOnImage elements in the whole page not only the ones in the block that triggered the event. 你之前的方式显然是在整个页面中找到所有site元素和所有textOnImage元素,而不仅仅是触发事件的块中的元素。

$("block").mouseenter(function(){        
    var $this = $(this);
    $this.find("site").fadeTo("slow", 0.25);
    $this.find("textOnImage").animate({ 
        opacity: "1"
    }, 600 );
});

$("block").mouseleave(function(){         
    var $this = $(this);
    $this.find("site").fadeTo("slow", 1);
    $this.find("textOnImage").animate({ 
        opacity: "0"
    }, 600 );
 });

You need to traverse into the structure of the hovered element. 您需要遍历悬停元素的结构。 You can use .find() like this... 你可以像这样使用.find() ......

$("block").mouseenter(function(){        
    $(this).find("site").fadeTo("slow", 0.25);
    $(this).find("textOnImage").animate({ 
        opacity: "1"
    }, 600 );
});

$("block").mouseleave(function(){         
    $(this).find("site").fadeTo("slow", 1);
    $(this).find("textOnImage").animate({ 
        opacity: "0"
    }, 600 );
 });

Within an event handler, this is a reference to the element to which the handler is bound. 在事件处理程序中, this是对处理程序绑定到的元素的引用。

You could have also used .children() instead of .find() since the targeted elements are only one level deep. 您也可以使用.find() .children()而不是.find()因为目标元素只有一层深度。


Side note, you can use .hover() , and pass two functions like this.. 旁注,你可以使用.hover() ,并传递这样的两个函数..

$("block").hover(function(){        
    $(this).find("site").fadeTo("slow", 0.25);
    $(this).find("textOnImage").animate({ 
        opacity: "1"
    }, 600 );
},
  function(){         
    $(this).find("site").fadeTo("slow", 1);
    $(this).find("textOnImage").animate({ 
        opacity: "0"
    }, 600 );
});

You'll probably also want to add some .stop() calls before your fadeTo and animate calls, otherwise when you mouse over and out rapidly, the animations will get backed up in a queue. 您可能还希望在fadeToanimate调用之前添加一些.stop()调用,否则当您快速鼠标移出时,动画将在队列中备份。

Try this (provide the this context to jquery selector to target the specific elements): 试试这个( this上下文提供给jquery选择器以定位特定元素):

$("block").mouseenter(function(){        
    $("site", this).fadeTo("slow", 0.25);
    $("textOnImage", this).animate({ 
        opacity: "1"
    }, 600 );
});

$("block").mouseleave(function(){         
    $("site", this).fadeTo("slow", 1);
    $("textOnImage", this).animate({ 
        opacity: "0"
    }, 600 );
 });

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

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