.append() not working in IE7
This scripts works in all browser's but IE7..
Overview: I got a page of products (li.prod). Each product has an image (li.prod div.mainpic img). That image has a title attribute which I am using to add a class of the same name to the appended . Then all the brands are shown automatically by using css.
$('li.prod .mainpic').each (function() {
var jThis = $(this);
var mainImages 开发者_如何学JAVA= jThis.find('img');
jThis.prepend('<div class="prod-logo"></div>');
var targList = jThis.find('div.prod-logo');
mainImages.each(function() {
newClass = $(this).attr('title');
targList.addClass (newClass);
});
});
Anyone know of a solution that will work for all browers?
I ran this fiddle in IE Tester's IE 7 emulator and had no issues. Give it a try and let me know if it works for you.
Version with prepend:
http://jsfiddle.net/jensbits/aBcVX/
Version with append:
http://jsfiddle.net/jensbits/aBcVX/2/
Version with appendTo:
http://jsfiddle.net/jensbits/aBcVX/3/
Version with after:
http://jsfiddle.net/jensbits/aBcVX/4/
The problem was not the code. I just needed to declare the variables.
var $jThis = null;
var $mainImages = null;
var $newClass = null;
var $targList = null;
$('li.prod .mainpic').each(function () {
var jThis = $(this);
var mainImages = jThis.find('img');
jThis.append('<div class="prod-logo"></div>');
var targList = jThis.find('div.prod-logo');
mainImages.each(function() {
newClass = $(this).attr('title');
targList.addClass(newClass);
});
});
精彩评论