jQuery event listener doesn't work in IE 7/8

2019-08-31 00:24发布

问题:

the problem is a bit tricky. My code to dynamically load table rows inside a table and then attach events to all the Table cells doesn't work in IE7/8. But sometimes it does work for no apparent reason.

Here is a little sample: http://jsbin.com/ivarus/6/edit

This is a simplified version of what I'm doing in my app

Here are the exact steps to repro: 1) Open IE and set to IE7/8 from dev tools (not necessary if you're already running IE8) 2) Navigate to http://jsbin.com/ivarus/6/edit Expected: clicks attached to TD work in the Output section Actually: the event listeners start working only after pressing "Run script"

In all other browsers (IE9+,Chrome,FF) this works immediately. Why? Am I doing something wrong ?Should I look for an alternative approach ?

回答1:

You need to wait for the document ready event before your script runs. Enclose your code like this.

<script>
$(function ()
    {
        //your code here
    });
</script>


回答2:

-- problem in append() not work on older IE

  if (($.browser.msie) & 
      (($.browser.version == '6.0') || ($.browser.version == '7.0')))
     {
       var oldHtml = $('#select').html();
       $('#select').html(oldHtml + appendInput);
     }