Jquery attr('src') undefined in IE 8 (works in FF)

前端 未结 7 1637
广开言路
广开言路 2021-01-18 07:22

This has gotten so far,that I will sum up what we found out:

  • Inside the event handler the attribute src cannot be read in IE8 (FF works fine), neither with

相关标签:
7条回答
  • 2021-01-18 07:45

    Bing Bang Boom,

    imgright = jQuery(".Image_right",this).attr('src');
    
    0 讨论(0)
  • 2021-01-18 07:45
    $(document).ready(function () {
        $("#imgReload").click(function () {
            $('#<%=imgCaptcha.ClientID %>').removeAttr("src");
            $('#<%=imgCaptcha.ClientID %>').attr("src", "Captcha.ashx");
        });
    });
    
    0 讨论(0)
  • 2021-01-18 07:54

    And why don't you easily use one working?

    alert(jQuery(this).children('#Image_center').attr('src'));
    

    change children to find

    alert(jQuery(this).find('#Image_center').attr('src'));
    

    It is probably the easiest solution, and when it work, why wouldn't you use it?

    0 讨论(0)
  • 2021-01-18 07:56

    This has gotten so far,that I will sum up what we found out:

    • Inside the event handler the attribute src cannot be read in IE8 (FF works fine), neither with jQuery nor with usual javascript

    • The only way to get the data was to get it outside the handler, write it to an array and read it afterwards from the inside of the handler

    • But there was still no possibility to write to src (neither jQuery nor javascript worked - only for IE 8)

    • I've got it working by writing the img elemts themselves to the document, but the reason behind this problem is no solved

    The new code

    relcounter = 1;
    imgleft_array = new Array();
    jQuery('.Image_left').each(function(){
        imgleft_array[relcounter] = jQuery(this).attr('src');
        relcounter++;
    });
    relcounter = 1;
    imgcenter_array = new Array();
    jQuery('.Image_center').each(function(){
        imgcenter_array[relcounter] = jQuery(this).attr('src');
        relcounter++;
    });
    relcounter = 1;
    imgright_array = new Array();
    jQuery('.Image_right').each(function(){
        imgright_array[relcounter] = jQuery(this).attr('src');
        relcounter++;
    });
    
    //view entry
    jQuery('.blogentry').live('click',function(){
        // Get contents
        entryindex = jQuery(this).attr('rel');
        blogtext = jQuery(this).children('.blogtext').html();
        blogauthor = jQuery(this).children('.onlyblogauthor').html();
        blogtitle = jQuery(this).children('.blogtitle').html();
        profileimage = jQuery(this).children('.profileimage').html();
        imgleft = imgleft_array[entryindex];
        imgcenter = imgcenter_array[entryindex];
        imgright = imgright_array[entryindex];
    
        // Write contents
        jQuery('#entryimages').html('');
        jQuery('#entryimages').html('<img class="rotate" width="132" height="138" id="bild_left" src="'+imgleft+'" /><img class="rotateright" width="154" height="162" id="bild_center" src="'+imgcenter+'" /><img class="rotate" width="132" height="138" id="bild_right" src="'+imgright+'" />');
    
        jQuery('.person').attr('src', profileimage);
        jQuery('#g_fb_name').html(blogauthor);
        jQuery('#g_titel').html(blogtitle);
        jQuery('#g_text').html(blogtext);
    
    });
    

    So I am just not using .attr('src') in the event handler....

    0 讨论(0)
  • 2021-01-18 07:56

    Try to make a delay:

    jQuery(document).ready(function() {
    
      setTimeout(function () {
    
        jQuery('.blogentry').each(function(){
          // your code...
        });
    
      }, 100); // if doesn't work, try to set a higher value
    
    });
    

    UPDATE

    Hope, this code will work.

    $('.blogentry img').each(function(){
      alert( $(this).attr('src') );
    });
    

    UPDATE

    I'm not sure, but maybe IE can't read classes with uppercase first letter... Try to change ".Image_center" to ".image_center"

    UPDATE

    Check your code again. You definitely have some error. Try this jsfiddle in IE8, attr('src') is showed correctly. http://jsfiddle.net/qzFU8/

    0 讨论(0)
  • 2021-01-18 08:01

    children looks for immediate child elements only where as find looks for all the elements within it until its last child element down the dom tree. If you are saying find is working that means the element you are looking is not its immediate children.

    Try to alert this jQuery(this).children('#Image_center').length see what you get.

    FYI. Even when any element is not found jQuery will return an emtpy object it will never be null. So alert an emtpy object will always give you [object Object]. You should alwasy check for the length property of the jQuery object.

    Try this

    alert(jQuery(this).find('#Image_center').length);//To check whether element is found or not.

    0 讨论(0)
提交回复
热议问题