简体   繁体   中英

e.preventDefault() is not working on div onclick

preventDefault() is not working on div onclick, i tried preventDefault and stopImmediatePropagation also nothing works, please see the below sample.

<title></title>

<script src="scripts/jquery-1.5.2.min.js" type="text/javascript"></script>

<script type="text/javascript">

    $().ready(function () {
        $("div").click(function (e) {
            e.preventDefault();
            //e.stopImmediatePropagation();
            return false;
        });

        //$("div").bind("click", function (e) { e.stopImmediatePropagation(); return false; });

    });

</script>

<div onclick="alert('1');">

    Sample text

</div>

There is no default behaviour of clicking on a div . What are you trying to prevent?

Also event.stopPropagation() is to stop the event from bubbling up ancestors and triggering their event handlers. What is it triggering in your case?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM