我正在使用jquery在我的页面上实现事件委托。我为顶级元素定义了一个单击处理程序,但只想在点击属于某个类的元素时执行“自定义”操作:
$("#myDivWithManyLinks").click(function(e){
var target = $(e.target);
if (target.hasClass('myClass123')
{
// do my "custom thing"
return false;
}
else
{
// XXX let the click be handled by the click handler that would otherwise get it
}
我该怎么做“XXX”?
感谢您的帮助,
拉拉
答案 0 :(得分:0)
编写一个处理函数,然后在else中引用它:
$("#myDivWithManyLinks").click(function(e){
var target = $(e.target);
if (target.hasClass('myClass123')
{
// do my "custom thing"
return false;
}
else
{
handler(e);
}
});
function handler(e){
// what you want all links that don't have your special class to do
}
答案 1 :(得分:0)
$("#myDivWithManyLinks").click(function(e){
var target = $(e.target);
if (target.hasClass('myClass123')
{
e.preventDefault();
//do custom stuff
}
});