0

div45452314234「 Facebookで共有」をクリックした場合の取得方法

<div id="div45452314234" class="Display">Display</div>
    <div class="Actions">
        <span>
            <div id="count">0</div>
            <a>Like</a>
        </span> 
        <span class="Author">Author</span>
        <span class="Recency">Recency</span> |
        <span><a class="ShareFB"  href="">Share on Facebook</a></span> |
        <span><a href="">Share on Twitter</a></span> |
    </div>

ここでヘックアウト:-

http://jsfiddle.net/WLS5T/

4

3 に答える 3

2

This will get you what you need...

$(function() {
    $(".ShareFB").on("click", function() {
        var $div = $(this).closest(".Actions").prev();
        alert($div.attr('id'));
    });
});

closest() will parse back up the DOM tree until it finds the first instance of an element with the class Actions and then prev() will select the previous element. $div will then be a jQuery object that you can use as you wish.

Edit

I've updated your fiddle...

http://jsfiddle.net/WLS5T/1/

One problem was the call to ShareFB which was stopping the rest of the script running, and I fixed my previously incorrect code as well.

于 2012-11-07T10:46:04.317 に答える
-1

これを試して:

$('.ShareFB').click(function(){
        alert($(this).parent().parent().prev().attr("id"));
    });
于 2012-11-07T10:52:28.630 に答える
-2

再帰を使用する必要があります。

function getTargetParent(node){
if($(node).attr('class')=='Actions'){
    alert($(node).prev().attr('id'));
}
 getTargetParent($(node).parent());
return;
}

 var targnode=$('.Actions').find('.ShareFB');//replace with $(this) in real scenario
 getTargetParent(targnode);

このjsfiddleを参照してください。http://jsfiddle.net/harendra/tAssd/

于 2012-11-07T11:03:19.877 に答える