开发者

jQuery div clicks, catching undesirable clicks

开发者 https://www.devze.com 2023-02-14 16:47 出处:网络
To make divs clickable I use this: $(\".clickable\").click(function (event) { window.location = $(this).find(\'a\').attr(\'href\');

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

精彩评论

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