最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I wrap a javascript event in a jQuery event? - Stack Overflow

programmeradmin4浏览0评论

I have this code:

<html>
<head>
    <script type="text/javascript" src=".3.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $('a.one').click(function(event){
                event.preventDefault();
            });
        });
        function test(event){
            event.preventDefault();
        }
    </script>
    <style type="text/css">
        a.test { font-weight: bold; }
        body { font-family:sans-serif; background-color:#AAAAAA;}
    </style>
</head>
<body>
    <a class="one" href="/">jQuery</a>
    <br/>
    <a class="two" href="/" onclick='test(event)'>stack overflow</a>
</body>
</html>

The test-function does not work as it stands now, since a regular javascript event doesn't support the jQuery event preventDefault-function. Is there some way to wrap a regular javascript event in a jQuery event so that I can use e.g. preventDefault?

I have this code:

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $('a.one').click(function(event){
                event.preventDefault();
            });
        });
        function test(event){
            event.preventDefault();
        }
    </script>
    <style type="text/css">
        a.test { font-weight: bold; }
        body { font-family:sans-serif; background-color:#AAAAAA;}
    </style>
</head>
<body>
    <a class="one" href="http://jquery.com/">jQuery</a>
    <br/>
    <a class="two" href="http://stackoverflow.com/" onclick='test(event)'>stack overflow</a>
</body>
</html>

The test-function does not work as it stands now, since a regular javascript event doesn't support the jQuery event preventDefault-function. Is there some way to wrap a regular javascript event in a jQuery event so that I can use e.g. preventDefault?

Share Improve this question asked Nov 25, 2009 at 10:42 CrosCros 4,3779 gold badges42 silver badges48 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 12

Try this:

function test(e) {
    $.Event(e).preventDefault();
}

Event object

I've found the best way to wrap a native event in a jQuery event is with fix:

event = $.event.fix(event);

Please note, this function is not part of the public API (although it really should be).

I think it may be the fact that you're passing event in with onclick='test(event)'. I think onclick='test' is enough. I could be wrong though.

Yes (see Darin's answer). You could also work around IE's lack of preventDefault instead (which is essentially what jQuery is doing):

if ('preventDefault' in event)
    e.preventDefault();
else
    e.returnValue= false;

When you just want to execute the javascript - and not redirect - when clicking the href use "return false" in your click function. For example:

$(function(){
        $('a.one').click(function(event){
                var condition = confirm('Do you want to redirect to ...?');
                return condition == true;
        });
});

If you never want the link to redirect use 'javascript:void(0);' as href attribute, all browsers will still render it as a link instead of an anchor (some IE version do this).

发布评论

评论列表(0)

  1. 暂无评论