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

JavaScriptjQuery problem on Internet Explorer 7 - Stack Overflow

programmeradmin0浏览0评论

I have a simple HTML page that looks like this:

...<div id="main">
    <a href="#">Click here!</a>
</div>...

I have a piece of jQuery JavaScript in the header that looks like this:

   <script type="text/javascript">
    $(document).ready(function() {
        DoHello();
    });
    
    function DoHello()
    {   
        $("div#main a").text("Click here!");
        $("div#main a").attr("onmouseup", "javascript:alert('Hello!');");
    }
</script>

When I click the HTML link in FireFox then I get an alert that says 'Hello!'. Why does this not work in IE7/8?

When I look at the (dynamically) build DOM in IE then I can see the onmouseup is present but it is never called. I have tried replacing onmouseup with onclick - same problem...

I have a simple HTML page that looks like this:

...<div id="main">
    <a href="#">Click here!</a>
</div>...

I have a piece of jQuery JavaScript in the header that looks like this:

   <script type="text/javascript">
    $(document).ready(function() {
        DoHello();
    });
    
    function DoHello()
    {   
        $("div#main a").text("Click here!");
        $("div#main a").attr("onmouseup", "javascript:alert('Hello!');");
    }
</script>

When I click the HTML link in FireFox then I get an alert that says 'Hello!'. Why does this not work in IE7/8?

When I look at the (dynamically) build DOM in IE then I can see the onmouseup is present but it is never called. I have tried replacing onmouseup with onclick - same problem...

Share edited Dec 25, 2022 at 14:08 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 20, 2009 at 12:31 Captain SensibleCaptain Sensible 5,0374 gold badges39 silver badges48 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

You shouldn't be using the JavaScript pseudo protocol for anything.

This should work:

function DoHello()
{   
    $("div#main a")
        .text("Click here!")
        .mouseup(function(){
             alert('Hello!');
        });
}

Don't use expando events, use jQuery!

$("div#main a").mouseup(function(){ alert('Hello!') });

instead of adding the onmouseup event like that why dont you just use the jQuery method like so:

$("div#main a").mouseup(function() {
   alert("hello");
});

should work :) for more info check out - http://docs.jquery./Events/mouseup

You should use the bind function like this:

function DoHello(){
$("div#main a").bind("mouseup", function(e){
    alert("Hello!");
});
}
发布评论

评论列表(0)

  1. 暂无评论