我想做一個if語句,如果按鈕的父母是父母的偶數孩子,做alert("it works");
,否則做alert("nope");
,這可能是一件非常簡單的事情,我真的很陌生! :)這裏是我當前的代碼:如何選擇「:偶」父母?
HTML:
<div>
<div>
<button>#0</button>
</div>
<div>
<button>#1</button>
</div>
<div>
<button>#2</button>
</div>
<div>
<button>#3</button>
</div>
</div>
JS:
$(function() {
$("button").click(function() {
var $parent = $(this).parent();
if ($parent.is(":even")) {
alert("it works");
} else {
alert("nope");
}
});
});
的jsfiddle:http://jsfiddle.net/goldson/Rn6gz/1/
提前感謝!
我正在考慮使用index()開始,可能會跟着它。爲什麼不會「:甚至」工作? – Jake