【问题标题】:Jquery attr('src') undefined in IE 8 (works in FF)Jquery attr('src') 在 IE 8 中未定义(在 FF 中工作)
【发布时间】:2013-06-07 19:11:34
【问题描述】:

到目前为止,我将总结我们的发现:

  • 在事件处理程序内部,属性 src 无法在 IE8 中读取(FF 工作正常),无论是使用 jQuery 还是使用通常的 javascript

  • 获取数据的唯一方法是在处理程序外部获取数据,将其写入数组,然后从处理程序内部读取数据

  • 但仍然无法写入 src(jQuery 和 javascript 都不起作用 - 仅适用于 IE 8)

  • 我已经通过将 img 元素本身写入文档来使其工作,但是这个问题背后的原因没有解决

我们的sn-p被使用了两次。

旧代码

<script type="text/javascript">
jQuery(document).ready(function() {
//...
//view entry
jQuery('.blogentry').live('click',function(){
    // Get contents
    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 = jQuery(this).children('.Image_left').attr('src');
    imgcenter = jQuery(this).children('.Image_center').attr('src');
    imgright = jQuery(this).children('.Image_right').attr('src');

    // Write contents
    jQuery('#bild_left').attr('src', imgleft);
    jQuery('#bild_center').attr('src', imgcenter);
    jQuery('#bild_right').attr('src', imgright);
    jQuery('.person').attr('src', profileimage);
    jQuery('#g_fb_name').html(blogauthor);
    jQuery('#g_titel').html(blogtitle);
    jQuery('#g_text').html(blogtext);
    //...
});
//...
// Change entry
jQuery('.blogentry').each(function(){
    entryindex = jQuery(this).attr('rel');
    if (entry == entryindex)
    {
        // The following works fine (so 'children' works fine):
        blogtext = jQuery(this).children('.blogtext').html();
        blogauthor = jQuery(this).children('.onlyblogauthor').html();
        blogtitle = jQuery(this).children('.blogtitle').html();
        profileimage = jQuery(this).children('.profileimage').html();

        // This does not work - only in IE 8, works in Firefox
        imgleft = jQuery(this).children('.Image_left').attr('src');
        imgcenter = jQuery(this).children('.Image_center').attr('src');
        imgright = jQuery(this).children('.Image_right').attr('src');

        //alert: 'undefined'
        alert(jQuery(this).children('.Image_center').attr('src'));

        //...
    }
}
//...
});
</script>

新代码

请查看我自己发布的新代码答案。

更新:

如果在点击事件中调用,这不起作用!!!

jQuery('.Image_left').each(function(){
alert(jQuery(this).attr('src'));
});

获取图像数据的解决方案:

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++;
});

//... inside the eventhandler (entryindex = 'rel' of blogentry):
imgleft = imgleft_array[entryindex];
imgcenter = imgcenter_array[entryindex];
imgright = imgright_array[entryindex];

这是可行的,因为它没有在事件处理程序内部调用,并且源是预先保存的

但是! 我仍然无法写入数据,这是我的目标:

jQuery('#bild_left').attr('src', imgleft);
jQuery('#bild_center').attr('src', imgcenter);
jQuery('#bild_right').attr('src', imgright);

更新!!!

这太疯狂了,我尝试通过普通的 javascript 编写数据。这也适用于 FF,但不适用于 IE8。属性 src 确实存在一些严重的问题:

document.getElementById('bild_left').src = imgleft;
document.getElementById('bild_center').src = imgcenter;
document.getElementById('bild_right').src = imgright;

alert(document.getElementById('bild_left').src);

这在FF中有效,但在IE8中无效,属性src在写入后仍然未定义!这似乎根本不是 jQuery 问题!

【问题讨论】:

  • 能否提供相关的html?
  • 尝试使用 "console.log(...)" 以及 ie/ff(firebug)/chrome 的 "developper tools" 而不是 "alert(...)"。您将能够详细检查该元素。 “[Object object]”可能意味着一个元素由 jquery 函数返回......或一个错误对象。所以控制台在这里很方便。
  • 我已经提供了html代码。到目前为止,我已经证明,返回了一个有效的对象。 console.log() 确实会抛出错误,这是否仅适用于某个插件?
  • 我认为你应该向我们展示整个点击功能。因为你的 sn-ps 自己没问题。
  • 我添加了代码并显示了两个处理程序。它使用了两次,但在两种情况下的作用相同......我现在通过用 html() 编写 img 元素来让它工作。非常感谢你。我想这将成为一个谜;-)

标签: javascript jquery internet-explorer-8


【解决方案1】:

children 仅查找直接子元素,而find 查找其中的所有元素,直到其在 dom 树下的最后一个子元素。如果您说find 正在工作,则意味着您正在查找的元素不是它的直接子元素。

试着提醒这个jQuery(this).children('#Image_center').length看看你得到了什么。

仅供参考。即使没有找到任何元素,jQuery 也会返回一个 emtpy 对象,它永远不会为空。所以提醒一个空对象总是会给你[object Object]。您应该始终检查 jQuery 对象的 length 属性。

试试这个

alert(jQuery(this).find('#Image_center').length);//检查是否找到元素。

【讨论】:

  • 他并不是说find 有效而children 无效。我认为find 示例只是应该表明该元素确实存在,而您答案的后半部分指出了为什么存在缺陷。他能够得到classid 所以他似乎得到了元素。
  • 我试图在我的答案的前半部分向他解释findchildren 之间的区别以及如何检查元素是否存在。
  • 对,我同意你所说的一切,只是不能解决他的问题。他得到了元素(因为他可以得到classid),而不是它的src
  • 感谢您的解释,我不知道 javascript 总是返回一个对象,但正如 kingjiv 所说,我可以读取类和 id 找到该对象。无论如何,我尝试了 'alert(jQuery(this).find('#Image_center').length);'它返回 1。所以是的,找到了对象。 'src' 似乎有问题。我也无法获得属性“alt”。
  • 对不起!我错了,我得到了属性“alt”,但只能使用 find()!!!但“src”仍未定义。通过使用孩子的“alt”也是未定义的。我真的不明白为什么....
【解决方案2】:

Bing Bang Boom,

imgright = jQuery(".Image_right",this).attr('src');

【讨论】:

  • 哇,这看起来很有希望,但仍然未定义 :-( 谢谢,不知道这个符号
  • 这也不行:jQuery('.Image_left').each(function(){ alert(jQuery(this).attr('src')); });
【解决方案3】:

你为什么不轻松地使用一个工作?

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

换孩子找

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

这可能是最简单的解决方案,当它起作用时,你为什么不使用它?

【讨论】:

  • 对不起,您误会了!该对象以任何一种方式与孩子一起返回并找到。但是 attr('src') 对于两种变体都保持为空 - 我已经尝试过 - 并且仅在 IE 8 中!
【解决方案4】:

问题不在于 attr('src') 而在于其他东西。以下 sn-p 在 IE8 中有效:

    <img id="xxx" src="yrdd">

    <script type="text/javascript">

        alert($('#xxx').attr('src'));

    </script>

但是,例如,如果您将 text/javascript 更改为 application/javascript - 此代码将在 FF 中有效,但在 IE8 中无效

【讨论】:

  • 仍然不是我的问题 :-/ 我已经有了“text/javascript”类型,但是谢谢! (我将在我的帖子中添加)
  • 我将 id 从图像更改为类(此时 id 错误),但这不是我问题的解决方案。但!我发现以下返回src: alert(jQuery('.blogentry .Image_left').attr('src'));我的问题,它似乎不适用于 jQuery(this),但我需要这个与已定义元素的连接。
【解决方案5】:

到目前为止,我将总结我们的发现:

  • 在事件处理程序内部,属性 src 无法在 IE8 中读取(FF 工作正常),无论是使用 jQuery 还是使用通常的 javascript

  • 获取数据的唯一方法是在处理程序外部获取数据,将其写入数组,然后从处理程序内部读取数据

  • 但仍然无法写入 src(jQuery 和 javascript 都不起作用 - 仅适用于 IE 8)

  • 我已经通过将 img 元素本身写入文档来使其工作,但是这个问题背后的原因没有解决

新代码

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);

});

所以我只是没有在事件处理程序中使用 .attr('src')....

【讨论】:

    【解决方案6】:

    尽量拖延:

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

    更新

    希望,这段代码可以工作。

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

    更新

    我不确定,但也许 IE 无法读取首字母大写的类... 尝试将“.Image_center”更改为“.image_center”

    更新

    再次检查您的代码。你肯定有一些错误。在 IE8 中尝试这个 jsfiddle,attr('src') 显示正确。 http://jsfiddle.net/qzFU8/

    【讨论】:

    • 我将值设置为 5000,但仍然没有成功。但!我发现以下返回 src(我已将 id 更改为类,检查我更新的问题): alert(jQuery('.blogentry .Image_left').attr('src'));我的问题,它似乎不适用于 jQuery(this),但我需要这个与已定义元素的连接。
    • 是的,谢谢。但这只能在事件处理程序之外调用。查看我的问题的结尾我现在通过将数据保存到数组中来获取数据,但我仍然无法编写它......任何想法,如果它不再是一种干净的方式都没关系:-/
    • 没有效果,大写字母无关紧要......但我已经假设了,因为我通过他们的类名循环了图像(查看我在顶部的帖子)。
    • 请再次查看我的帖子。我使用普通的 javascript 编写数据,它在 FF 中有效,但在 IE8 中无效!这不是 jQuery 的问题,使用 javascript 也不能​​正常工作!
    【解决方案7】:
    $(document).ready(function () {
        $("#imgReload").click(function () {
            $('#<%=imgCaptcha.ClientID %>').removeAttr("src");
            $('#<%=imgCaptcha.ClientID %>').attr("src", "Captcha.ashx");
        });
    });
    

    【讨论】:

    • 有趣的想法,我没有尝试事先删除该属性,但我不再在相关项目上工作,所以我无法测试,如果这是解决问题的方法。注意:在其他项目中,我更改了 src 属性没有问题,即使在 IE8 中也是如此 - 所以这是相当具体的情况,原因仍然未知。但也许你的回答会有所帮助。
    猜你喜欢
    • 2010-12-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 2012-09-21
    • 2010-12-20
    • 1970-01-01
    相关资源
    最近更新 更多