javascript - Jquery attr ('src' ) 在 IE 8 中未定义(在 FF 中有效)

标签 javascript jquery internet-explorer-8

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

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

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

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

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

我们的代码片段被使用了两次。

旧代码

<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 问题!

最佳答案

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

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

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

试试这个

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

关于javascript - Jquery attr ('src' ) 在 IE 8 中未定义(在 FF 中有效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7162794/

相关文章:

javascript - 带有网格的 jQuery 可调整大小居中 div 产生意想不到的结果

javascript - 如何向我的应用添加静音按钮?

jquery - 如何使用 Jquery Masonry 和 Twitter Bootstrap 使 Twitter bootstrap 中的 <divs> 以 Masonry 格式显示?

css - 如何检查CSS是否对IE8有效

javascript - window.location.href 在 IE8 中不起作用

javascript - SVG鼠标事件在Firefox4中有效,但在IE8中无效

javascript - 进行 ajax 调用后调用方法时出现问题

javascript - 功能的成功部分不起作用

jquery - WordPress 固定链接

javascript - 当应用程序托管在 IIS 上的虚拟目录中时,如何将 Request 的 ApplicationPath 传递给 Requirejs config.path 的 baseUrl?