2017-12-22 8 views
0

私はtd内にスパンを持っています。スパンをクリック可能にすることはできますが、親をクリック可能にすることに問題があります。動的要素の親をクリック可能にするjquery

これは、私はこれは親のために働くために取得しようとしていますスパン

$(document).on('click', '.glyphicon-question-sign', function(){ 
      alert("test"); 
}); 

のために働く、それはしていません。

$(document).on('click', '.glyphicon-question-sign:parent', function(){ 
      alert("test"); 
}); 

ここには動的に作成されたhtmlがあります。

<td><span class="glyphicon glyphicon-question-sign text-warning"> 
</span> Unavailable 
</td> 
+0

?私が知っている限り、すべてのセレクタは下向きまたは横向きで、上向きではありません。 – Taplar

+0

@Taplar https://api.jquery.com/parent-selector/。明らかにそれは働いていないが、私は別の方法があるかどうか疑問に思っている/ – Aaron

+0

あなたはどの特定の項目を選ぶべきですか? – BlackStar1991

答えて

2

セレクタ:has()セレクタを使用して、特定のクラスの要素を含むtdに一致させることができます。 parent`セレクター:あなたは `のために読んで何をしたかのAPI

$(document).on("click", "td:has(.glyphicon-question-sign)", function() { 
 
    alert("test"); 
 
})
td { 
 
    width: 100%; 
 
    border: 5px solid #000; 
 
} 
 

 
span { 
 
    display: block; 
 
    height: 40px; 
 
    width: 100px; 
 
    background-color: #f00; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <td> 
 
    <span class="glyphicon glyphicon-question-sign text-warning"></span> 
 
    </td> 
 
</table>

1

$("span").on("click", function() { 
 
    $(this).parent("td").css("border-color", "yellow"); 
 
})
td { 
 
    width: 100%; 
 
    border: 5px solid #000; 
 
} 
 

 
span { 
 
    display: block; 
 
    height: 40px; 
 
    width: 100px; 
 
    background-color: #f00; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <td> 
 
    <span class="glyphicon glyphicon-question-sign text-warning"></span> 
 
    </td> 
 
</table>

私は私が正しくあなたを理解わかりません。私はそうする

+0

ありがとうございますが、親要素をクリック可能にしようとしています。子がクリックされたときに親に変更を加えない。 – Aaron

+0

気にしないで、私は親要素を使って、最初の子供に行くのではなく、子供から泡立ちます。 $(document).on( 'click'、 'tr.warning td:first-child'、function(){ alert( "test"); }); – Aaron

関連する問題