开发者

JQuery and Internet Explorer - JQuery not working

I have a little script that replaces text with images depending on the value in a Wordpress custom field. It works perfectly fine in all browsers apart from IE.

My code is:

$(document).ready(function() {
$('.CustomRow').each(function(i, e) {
    if (m = (text = $(e).text().trim()).match(re = /: +(yes|no)$/)) {
        var class = $('span', e).text().replace(/[^\w]/g, '').toLowerCase();
        $(e)
            .addClass(class)
//              .text(text.replace(re, ''))
        ;
    }

});
if ($('.levelparking').html().match(/yes/)) {
    $('.levelparking').html('<img src="/wp-content/themes/directorypress/thumbs/level_ground.jpg">');
} else {
    $('.levelparking').hide();
    }

if ($('.water').html().match(/yes/)) {
    $('.water').html('<img src="/wp-content/themes/directorypress/thumbs/water.jpg">');
}else {
    $('.water').hide();
    }

 if ($('.greywater').html().match(/yes/)) {
    $('.greywater').html('<img src="/wp-content/themes/directorypress/thumbs/grey_water.jpg">');
}else {
    $('.greywater').hide();
    }

 if ($('.blackwater').html().match(/yes/)) {
    $('.blackwater').html('<img src="/wp-content/themes/directorypress/thumbs/black_water.jpg">');
}else {
    $('.blackwater').hide();
    }

 if ($('.electric').html().match(/yes/)) {
    $('.electric').html('<img src="/wp-content/themes/directorypress/thumbs/electricity.jpg">');
}else {
    $('.electric').hide();
    }

if ($('.extranight').html().match(/yes/)) {
    $('.extranight').html('<img src="/wp-content/themes/directorypress/thumbs/xtra_night.jpg">');
}else {
    $('.extranight').hide();
    }

if ($('.dogwalks').html().match(/yes/)) {
    $('.dogwalks').html('<img src="/wp-content/themes/directorypress/thumbs/dog_walks.jpg">');
}else {
    $('.dogwalks').hide();
    }

if ($('.realales').html().match(/yes/)) {
    $('.realales').html('<img src="/wp-content/themes/directorypress/thumbs/real_ales.jpg">');
}else {
    $('.realales').hide();
    }

    if ($('.busstop').html().match(/yes/)) {
    $('.busstop').html('<img src="/wp-content/themes/directorypress/thumbs/bus_stop.jpg">');
}else {
    $('.busstop').hide();
    }

    if ($('.wifi').html().match(/yes/)) {
    $('.wifi').html('<img src="/wp-content/themes/directorypress/thumbs/wifi.jpg">');
}else {
    $开发者_如何学Go('.wifi').hide();
    }


});

I'm getting the following error message in IE Developer Tools:

Expected Identifier - Line 4, character 17.

This relates to this line:

var class = $('span', e).text().replace(/[^\w]/g, '').toLowerCase();

Any idea why this is happening?

Thanks in advance


"var class" is most likely the problem because class is a reserved keyword in IE.

The solution is to use another variable name.


class is a reserved keyword in Javascript (ECMAScript), use a different variable name.

The reason that it works in some browsers is that they allow some keywords as identifiers in some situations. Naturally you should not use keywords as identifiers at all.


My bet would be on the regular expression. You may want to dig into IE's regular expression support and make sure that it is not slightly different from the other browsers... as is often the case with IE.


$('span', e) is null hence it will not work,. Post HTML or a jsfiddle sample.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜