2016-11-12 92 views

回答

1

當點擊箭頭,你感興趣的事情是箭頭的父母的兄弟姐妹。

$("body").on('click tap', '.sort-arrows .fa-arrow-down', function(e) { 
 
    e.preventDefault(); 
 

 
    var $flow = $(this).parent().siblings("div.flow-content:first"); 
 

 
    // ====================== 
 
    // also works but more brittle 
 
    // ====================== 
 
    // var $flow = $(this).parent().next(); 
 
    // ====================== 
 

 
    console.log($flow.data("key")); 
 
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous" /> 
 

 
<div class="sort-arrows"> 
 
    <span class="fa fa-arrow-up" id="sort-up"></span> 
 
    <span class="fa fa-arrow-down" id="sort-down"</span> 
 
</div> 
 

 
<div class="flow-content" data-key="2yqz1jpdxwy2g434zyo8r7k6vml9n50d"> 
 
    <h3 class="bg-primary">Title</h3> 
 
</div> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

非常感謝您的幫助。它效果很好。 –

-1
$(this).attr("data-key") 

$(this).data("key") 
+0

仍然給我不確定。 –

+0

處理程序位於箭頭元素上,而不是小提琴中的流內容元素。 $(this)不是@mayersdesign期望它在這裏。 – JonSG

+0

非常正確,我的(草率!)答案在這裏作爲例證 – mayersdesign

1

jsfiddle

$("body").on('click tap', '.sort-arrows .fa-arrow-down', function(e) { 
 
    e.preventDefault(); 
 
    var elem = $(this).parent().attr("class"); 
 
    var data = $("." + elem).next(".flow-content").attr("data-key"); 
 
    alert(data); 
 
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous" /> 
 

 
<div class="sort-arrows"><span class="fa fa-arrow-up" id="sort-up"></span><span class="fa fa-arrow-down" id="sort-down"></span></div> 
 

 
<div class="flow-content" data-key="2yqz1jpdxwy2g434zyo8r7k6vml9n50d"> 
 
    <h3 class="bg-primary">Title</h3> 
 
</div> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

相關問題