给定 HTML,例如
<div class="tpl grey">Hosts:
<p>Hi !</p>
<p>How are you ?</p>
<p>What ever.</p>
<a href="./~">An other child & element type !</a>
</div>
如何使单击子元素切换class="grey"
最近的父.tpl
元素?
以下代码失败:
//Action to do on parent $(".tpl")
var switch1 = function () {
$(this).closest(".tpl").toggleClass("blue grey");
}
// On() click event
$(document).ready(function() {
$(".tpl").on("click", "p", switch1() );
});
小提琴:http: //jsfiddle.net/MRcCy/1