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

internet explorer - onmouseover doesn't work when using javascript to add img tag on IE - Stack Overflow

programmeradmin4浏览0评论

I need some javascript code that dynamically adds an img tag to a div, and the img tag needs onmouseover and onmouseout handlers.

I have it working on Firefox. But it doesn't quite work on IE. On IE, the img tag is added, but the onmouseover and onmouseout handlers are not active.

Here's the code:

<body>  
    <div id='putImageHere' />  

    <script type='text/javascript'>
        var node = document.getElementById('putImageHere');
        var img = document.createElement('img');
        img.setAttribute('src', '.png');
        node.appendChild(img);

        // first attempt, which works on Firefox but not IE
        img.setAttribute('onmouseover', "alert('mouseover')");
        img.setAttribute('onmouseout', "alert('mouseout')");

        // second attempt, which I thought would work on IE but doesn't
        img.addEventListener('mouseover', function() { alert('mouseover') }, false);
        img.addEventListener('mouseout', function() { alert('mouseout') }, false);
    </script>  
</body>  

I need some javascript code that dynamically adds an img tag to a div, and the img tag needs onmouseover and onmouseout handlers.

I have it working on Firefox. But it doesn't quite work on IE. On IE, the img tag is added, but the onmouseover and onmouseout handlers are not active.

Here's the code:

<body>  
    <div id='putImageHere' />  

    <script type='text/javascript'>
        var node = document.getElementById('putImageHere');
        var img = document.createElement('img');
        img.setAttribute('src', 'http://sstatic/so/img/logo.png');
        node.appendChild(img);

        // first attempt, which works on Firefox but not IE
        img.setAttribute('onmouseover', "alert('mouseover')");
        img.setAttribute('onmouseout', "alert('mouseout')");

        // second attempt, which I thought would work on IE but doesn't
        img.addEventListener('mouseover', function() { alert('mouseover') }, false);
        img.addEventListener('mouseout', function() { alert('mouseout') }, false);
    </script>  
</body>  
Share Improve this question asked Nov 12, 2009 at 18:26 Mike WMike W 7472 gold badges7 silver badges10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8
if (img.addEventListener) {
    img.addEventListener('mouseover', function() {}, false);
    img.addEventListener('mouseout', function() {}, false);
} else { // IE
    img.attachEvent('onmouseover', function() {});
    img.attachEvent('onmouseout', function() {});
}

look into using one of the many popular javascript libraries (jquery, prototype, etc). they hide browser inconsistencies so you don't need to worry about writing code like above.

img.setAttribute('src', 'http://sstatic/so/img/logo.png');

Don't use setAttribute on an HTMLElement, at all. There are bugs in it in Internet Explorer and it doesn't get you anything pared to the more readable DOM Level 1 HTML alternative:

img.src= 'http://sstatic/so/img/logo.png';

The essential problem with setAttribute on IE is that it works the same as normal property access, even when the property has a different name or type to the attribute. So this:

img.setAttribute('onmouseover', "alert('mouseover')");

is the same as saying:

img.onmouseover= "alert('mouseover')";

which makes no sense: the onmouseover property is, like all event handlers, supposed to be a function, not a string. Assign a function instead:

img.onmouseover= function() {
    alert('mouseover');
};

and then you've also got rid the nasty eval-style practice of putting code inside a string. Hooray!

As for addEventListener, you can use it if you add a fallback attachEvent call for IE (which has its own crazy event listener system). But in the mon case where you only ever have one listener for each element it is much easier to stick with the old-school supported-by-every-browser onsomething-assigning event handlers.

Turns out IE doesn't support the addEventListener method. You can check a workaround here.

Anyway, why don't you use jQuery? It almost overrides all the patibility issues, has a handful of extras and it totally rocks.

Ob's answer shows the correct method for attaching event listeners without using a JavaScript library (and was first), but I would like to add the following:

onmouseover/out should never be attached as attributes. Basically, you're gone through all the trouble of not doing

just to do it in your JavaScript. Event handlering callbacks should always be attached as event listeners via attachEvent for IE, and addEventListener for pretty much everyone else.

发布评论

评论列表(0)

  1. 暂无评论