开发者

jquery addClass IE7 Performance Issue

I am facing a strange scenario only in IE7 like, adding a class to an ID.

$(idName).addClass("sel");

If my idName is short then I am not facing any issue but if it is very long then browser is hanging.

At the time of hanging, idName is like dateRange(2006,2007,2008,2009,2010)

Edited:

popu开发者_运维问答latedID = "dateRange(2006,2007,2008,2009,2010)";
var idName = "li[id=\"" + populatedID + "\"]";
$(idName).addClass("sel");

Please suggest me any alternative to work with it.

Thanks in advance.


that is an odd looking ID which would equate to id="dateRange(2006,2007,2008,2009,2010)" if it were in-line and thus invalid as ID can consist of only alpha numeric plus "-" and "_" and begin with an alpha

http://www.w3.org/TR/html401/types.html#type-name


One possible problem is that you're using characters that are not strictly permitted in IDs. While it may work in some browsers, you can't count on it -- especially in a browser as old as IE7.

As an experiment, perhaps you can try modifying these IDs, replacing the parentheses and commas with underscores and hyphens.

If you're using the ID to store data, a far better option is to use the data() function. When generating the HTML, stash your date ranges like this:

<li data-daterange="2006,2007,2008,2009">Something</li>

Then make a function to apply your styling logic like this:

var highlightYear = function (year) {
  $('#your_ul').find('li').filter( function(){
    return $.inArray( year, $(this).data('daterange').split(',') ) > -1;
  }).addClass('sel');
};

Then use:

highlightYear( 2006 ); 
// adds "sel" class to all LI elements with "2006" in "daterange"

References: W3C HTML Specification on name and ID tokens.


i have never seen an id with

(,)

can you try using alphanumerics and seperate with hyphens or underscores or camelcase?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜