在IE 8中未定义Jquery attr('src')(在FF中工作) [英] Jquery attr('src') undefined in IE 8 (works in FF)

查看:145
本文介绍了在IE 8中未定义Jquery attr('src')(在FF中工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


  • 在事件处理程序中的属性在IE8中无法读取src(FF工作正常),jQuery也没有和通常的javascript一样

  • 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

但是仍然没有可能写入src(jQuery和javascript都没有)工作 - 仅适用于IE 8)

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

我通过将img elemts自己写入文档来实现它,但这个问题背后的原因并没有解决

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

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

The snippet we have is used twice.

旧代码

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

新代码

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

Please see my own posted answer for the new code.

更新:

如果在click事件中调用,那么工作!!!

This does not work if called inside of the click event!!!

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

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

SOLUTION TO GET THE IMAGE DATA:

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

这是有效的,因为它未在事件处理程序中调用,并且事先保存了源

This works because it is not called inside the event handler and the sources are saved beforehand

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

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

UPDATE !!!

UPDATE!!!

这是只是疯了,我试图通过通常的JavaScript编写数据。这也适用于FF,但在IE8中没有。这里的属性src确实存在一些严重的问题:

This is just crazy, I tried to write the data via usual javascript. This also works in FF, but no in IE8. Here really is some serious problem witt the attribute 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问题!

This works in FF, but not in IE8, the attribute src remains undefined after writing! This seems to be not a jQuery problem at all!

推荐答案

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

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


  • 在事件处理程序中,无法在IE8中读取属性src(FF工作正常),jQuery也没有通常的javascript

  • 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

但是仍然没有可能写入src(jQuery和javascript都没有工作 - 仅适用于IE 8)

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

我已经通过将img elemts自己写入文档来实现它,但这个问题背后的原因还没有解决

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

新代码

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')....

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

这篇关于在IE 8中未定义Jquery attr('src')(在FF中工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆