开发者

IE won't split string with Javascript

I have the following code:

$('#smallcart .plusone').live('click',function(){
   var id = $(this).attr('id');
   articlenr = id.split('_')[1];
});

this works fine in FF, safari, Chrome, how开发者_开发知识库ever in IE (7 and 8) it throws an error on the split-function (this property or method is not supported by this object).

if I alert the 'id'-variable I get something like plus_5751. (so I want to get the '5751' part) if I do alert(typeof(id)) I get String as an answer...

Maybe somebody can point me to the right answer?

Thx


The split works pretty well in IE. The problem is the part left of the equal-sign. It's an object with all input-fields having the name articlenr and therefor IE quits with 'this property or method is not supported by this object' when you're trying to assign a string to it.


Your code works just fine for me in Internet Explorer - as it should be expected to. The problem must lie elsewhere, perhaps something is overriding String.prototype.split?. You can see a working example of your code at http://jsfiddle.net/AndyE/6K77Y/. The first thing to check for is any Internet Explorer specific code in your scripts.

I would make one small improvement for efficiency. $(this).attr('id'); is pretty much the long winded way of writing this.id. It's slower, because a new jQuery object has to be created and then the attr function has to run. Without it, your code can be compressed more, whilst still remaining very readable, if you like:

$('#smallcart .plusone').live('click',function(){
   articlenr = this.id.split('_')[1];
});


Try renaming your variable 'id' to something else. IE doesn't like it when you name things in your scripts the same as items in the DOM.

Never mind, that seems to have not been the issue in this case. I have, however, had issues in the past with IE specific bugs caused by variable names.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜