最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

JavaScriptjQuery problem on Internet Explorer 7 - Stack Overflow

matteradmin3PV0评论

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!");
});
}
Post a comment

comment list (0)

  1. No comments so far