English 中文(简体)
jquery 选择以前的配对元件
原标题:jquery select previous matched element that isnt a sibling
  • 时间:2011-10-16 20:15:10
  •  标签:
  • jquery

这似乎是一个非常棘手的问题,但我有一个超文本的OM结构,如:

<div class="1">
    <div class="1a">
       <div class="1aa"></div>
    </div>
    <div class="1b">
        <a href="#">Link</a>
    </div>
</div>

我需要挑选到<代码><div>,该编码为直接兄弟姐妹,例如:

$(function(){
   $( a ).click(function(){
       // select div .1a
   });
});

I can t directly select div .1a because there may be multiple instances of .1a and I need to select the one thats nearest to the link.

任何帮助

问题回答

使用。 这将吸引最接近图像的所需四分之三。

$(function(){
   $( a ).click(function(){
     $(this).closest(".1a");
   });
});

.closest (“selector”)发现一个与特定甄选者相当的元素的头祖先

See http://api.jquery.com/closest/ for more info and examples

您能否明确地将应选择的四分五分之四放在链接的左侧:

<div class="1">
<div class="1a" id="my-div-1">
   <div class="1aa"></div>
</div>
<div class="1b">
    <a href="#" rel="my-div-1">Link</a>
</div>
</div>

您的《共同法》将考虑:

$(function(){
   $( a ).click(function(){
     var selector = $(this).attr("rel");
     $("#"+selector).DOSOMETHING();
   });
});

这将永远发挥作用,而不需要考虑是最接近的。

Jovan





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签