简体   繁体   English

我如何从jquery中的多个div中选择特定的div

[英]How i can select specific div from multiple divs in jquery

I have something like this, and i need to show every div called "plink" just in the main div of each parent, so i tried to fadeIn ".plink" but its doing the same function for all the divs of "plink" 我有这样的东西,我需要在每个父节点的主div中显示每个div称为“plink”,所以我试图淡入“.plink”但它为“plink”的所有div执行相同的功能

<script>

    $(document).ready(function(){
        $('.plink').hide();
        $('.project').mouseover(function(){
            $(this).next('.plink').fadeIn(400);
        });
        $('.project').mouseleave(function(){
            $(this).next('.plink').fadeOut(200);
        });

    });

</script>

<div class="project">
    <div class="plink">
        <div class="go"></div>
        <div class="goplus"><img src="images/more.png" border="0"/></div>
    </div>
    <div class="pic"><img src="images/portfolio_pic2.png" border="0" alt="projectname"/></div>
    <div class="title">Test1</div>
</div>
<div class="spacer_project"></div>
<div class="project">
    <div class="plink">
        <div class="go"></div>
        <div class="goplus"><img src="images/more.png" border="0"/></div>
    </div>
    <div class="pic"><img src="images/portfolio_pic.png" border="0" alt="projectname"/></div>
    <div class="title">test2</div>
</div>

You can use find() instead of next() ... 您可以使用find()而不是next() ...

$(this).find('.plink').fadeIn(400);

because this is your .project div then you need to "find" the child elements that you are looking for. 因为this是你的.project div,所以你需要“找到”你正在寻找的子元素。 Using next() means you will get the very next element if it matches the selector (ie it is check to see if the next .project div matches the .plink selector) 使用next()意味着如果匹配选择器,你将获得下一个元素(即检查下一个.project div是否与.plink选择器匹配)

I would go the FIND route like musefan suggested. 像musefan建议的那样,我会去找FIND路线。 Here is the solution code: 这是解决方案代码:

http://jsfiddle.net/bx7YC/ http://jsfiddle.net/bx7YC/

<div class="project">
    <div class="plink">
        <div class="go">go</div>
        <div class="goplus">goplus</div>
     </div>
     <div class="pic">pic</div>
     <div class="title">Test1</div>
</div>

<div class="spacer_project"></div>

<div class="project">
    <div class="plink">
        <div class="go">go</div>
        <div class="goplus">goplus</div>
    </div>
    <div class="pic">pic</div>
    <div class="title">Test2</div>
</div>​

$('.plink').hide();

$('.project').mouseover(function(){
  $(this).find('.plink').fadeIn(400);
});
$('.project').mouseleave(function(){
  $(this).find('.plink').fadeOut(200);
});​

I replaced the broken img links with simple text for the jsfiddle. 我用jsfiddle的简单文本替换了破碎的img链接。

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

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