2014-05-21 56 views
-2

對於HTML如何獲得警報按鈕時,點擊裏面的div具體 - Jquery的

<div class='abc'> 

    <button type="button">A</button> 
    <button type="button">B</button> 
    <button type="button">C</button> 
    <button type="button">D</button> 

</div> 

<button type="button">X</button> 
<button type="button">Y</button> 
<button type="button">Z</button> 

我希望當有類名abc格按下按鈕中的任何一個提醒。

假設有人點擊A或B,應該會出現提示。

+0

'的jQuery( 'ABC鍵')點擊(...)' –

+0

爲什麼否決票給出? – user3636511

+0

+1現在快樂..? –

回答

3

您需要選擇器來限制該元素內的按鈕。試試這個:

$('.abc button').click(function() { 
    alert('foo'); 
}); 

如果您特別想將其限制在AB按鈕,你需要一個filter()

$('.abc button').filter(function() { 
    return $(this).text() == "A" || $(this).text() == "B"; 
}).click(function() { 
    alert('foo'); 
}); 
1

嘗試使用direct children選擇,也可以使用後代選擇(」。 abc button')

$('.abc > button').click(function(){ 
alert(''); 
}) 
+0

您是否在.abc內尋找div? – Adil

+0

@Adil錯字匆忙..:\ –

0

您可以通過此方法選擇div下的按鈕..

$('.abc button').click(function() { 
    alert('Hii'); 
}); 
0

使用屬性類型選擇選擇所有按鈕

$(".abc [type=button]").on("click",function(e){ 
     alert(1); 
    }); 
+0

'使用選擇器什麼選擇器...? –

+0

你能解釋一下你的代碼片段來幫助網站的提問者和未來訪問者嗎? –

0

您應該委託事件容器級別只使用一個處理程序:

$('.abc').on('click', 'button', function(){ 
    alert("Well next time I'll read some basic tutos instead of posting this kind of question on SO..."); 
}); 
0

這種方式,您可以撥打單擊事件特定div元素的按鈕具有指定「abc」類別

$("div[class*='abc'] :input").click(function() { 
    alert('your message'); 
}); 

check my fiddle

1

您可以使用:

$(".abc").find("button").click(function() { 
     alert(""); 
}); 
+0

+1。使用'.find()' –

+0

@ A.Wolff謝謝,必須得到創意,因爲我去回答它,然後快速刷新,並有一堆答案哈哈 – Livewire

相關問題