开发者

jQuery div clicks, catching undesirable clicks

To make divs clickable I use this:

$(".clickable").click(function (event) {
    window.location = $(this).find('a').attr('href');
    event.preventDefault();
});

I'm using an <asp:Repeater> in asp.NET to create several such <div>s.

The problem is that all clicks in the div are picked up by this jQuery - I also have an <asp:Button> inside the div, for this I want to catch the click as normal and process it in the repeater_ItemCommand event - But this doesn't fire - the page just redirects to the href found in the hyperlink in the div.

An开发者_StackOverflowyway I can stop this and make the button pick up as normal?


You need to check whether event.target is an input element (use the nodeName property, or call $(event.target).is(...)).

If it is, don't do anything in your handler.


One way of doing it would be to modify your markup and add another div inside of your main div that has the anchor tag. Give that the class clickable. And have your button outside of it, like so.

<div>
    <div class='clickable'>
        <a href='#'>Link</a>
   </div>
   <asp:button>
</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜