开发者

jQuery .trim() IE Browser Compatibility Question

开发者 https://www.devze.com 2023-02-16 12:29 出处:网络
I tested the following in FF, OP, Chrome, Safari and IE. It works in them all except the 3 IEs I tested: 8, 7, and 6.

I tested the following in FF, OP, Chrome, Safari and IE. It works in them all except the 3 IEs I tested: 8, 7, and 6.

// truncate testimonial 
var visiblePara = $('div.bannerUnder p.show');
if (visiblePara.text().trim().length > 150) {
    var text = visiblePara.text().trim();
    var author = $('div.bannerUnder p.show > strong').text();
    text = text.substr(0, 150) + "...";
    visiblePara.text(text).append("<strong>" + author + "</strong>");
}

It says:

Object doesn't support this property or method and points to this line:

if (visiblePara.text().trim().length > 150) {
开发者_Go百科

What could be the issue?


Try changing:

visiblePara.text().trim().length

to:

$.trim(visiblePara.text()).length

You can even move the text variable up, with something like this:

// truncate testimonial 
var visiblePara = $('div.bannerUnder p.show');
var text = $.trim(visiblePara.text());
if (text.length > 150) {
    var author = $('div.bannerUnder p.show > strong').text();
    text = text.substr(0, 150) + "...";
    visiblePara.text(text).append("<strong>" + author + "</strong>");
}


trim is not a method of String.prototype until IE 8. It has existed in other browsers for a while now.

I tried it in IE8 and it worked for me. Use jQuery.trim() jQuery.trim(str) instead

0

精彩评论

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