Jquery attr('src') 在 IE 8 中未定义(在 FF 中工作)
到目前为止,我将总结我们发现的内容:
- ,都无法在 IE8 中读取属性 src(FF 工作正常)
获取数据的唯一方法是在处理程序之外获取它,将其写入数组并随后读取从里面handler
但是仍然无法写入 src (也没有jQuery 和 javascript 都有效 - 仅适用于 IE 8)
我通过编写 img elemts 使其工作自己写入文档,但这个问题背后的原因没有解决
我们的代码片段被使用了两次。
旧代码
<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 的问题!
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 snippet we have is used twice.
The old code
<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>
The new code
Please see my own posted answer for the new code.
UPDATE:
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
BUT! I still cannot write the data, which is my aim:
jQuery('#bild_left').attr('src', imgleft);
jQuery('#bild_center').attr('src', imgcenter);
jQuery('#bild_right').attr('src', imgright);
UPDATE!!!
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);
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
children
仅在find
查找其中的所有元素直到其在 dom 树中的最后一个子元素之前查找直接子元素。如果您说find
正在工作,则意味着您正在查找的元素不是其直接子元素。尝试提醒此
jQuery(this).children('#Image_center').length
看看你会得到什么。供参考。即使没有找到任何元素,jQuery 也会返回一个空对象,它永远不会为 null。因此,警报空对象将始终为您提供
[object Object]
。您应该始终检查 jQuery 对象的length
属性。试试这个
alert(jQuery(this).find('#Image_center').length);
//检查是否找到元素。children
looks for immediate child elements only where asfind
looks for all the elements within it until its last child element down the dom tree. If you are sayingfind
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 thelength
property of the jQuery object.Try this
alert(jQuery(this).find('#Image_center').length);
//To check whether element is found or not.宾邦繁荣,
Bing Bang Boom,
为什么不轻松地使用一个工作呢?
改变孩子们去寻找
它可能是最简单的解决方案,当它起作用时,你为什么不使用它呢?
And why don't you easily use one working?
change children to find
It is probably the easiest solution, and when it work, why wouldn't you use it?
问题不在 attr('src') 中,而是在其他地方。以下代码片段在 IE8 中有效:
但是,如果您将 text/javascript 更改为 application/javascript - 此代码将在 FF 中工作,但在 IE8 中不起作用
the problem is not in the attr('src') but in something else. The following snippet works in IE8:
But if you for example change the the text/javascript to application/javascript - this code will work in FF but will not work in IE8
到目前为止,我将总结我们发现的内容:
获取数据的唯一方法是在处理程序之外获取它,将其写入数组并随后读取从里面handler
但是仍然无法写入 src (也没有jQuery 和 javascript 都有效 - 仅适用于 IE 8)
我通过编写 img elemts 使其工作自己写入文档,但这个问题背后的原因没有解决
新代码< /strong>
所以我只是不在事件处理程序中使用 .attr('src') ....
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
So I am just not using .attr('src') in the event handler....
尝试延迟:
更新
希望,此代码能够工作。
更新
我不确定,但也许 IE 无法读取首字母大写的类......
尝试将“.Image_center”更改为“.image_center”
更新
再次检查您的代码。你肯定有一些错误。在 IE8 中尝试这个 jsfiddle,attr('src') 显示正确。 http://jsfiddle.net/qzFU8/
Try to make a delay:
UPDATE
Hope, this code will work.
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/