我将尝试创建一个 if 语句,当按钮的父元素是其父元素的偶数子元素时,执行
HTML:
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/
提前致谢!