admin管理员组文章数量:1303055
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, however 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
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, however 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
Share Improve this question edited Oct 19, 2010 at 8:12 Timothy S. Van Haren 8,9662 gold badges31 silver badges34 bronze badges asked Oct 19, 2010 at 8:07 IanIan 431 silver badge3 bronze badges 5- 1 possible duplicate of stackoverflow./questions/1453521/… – ArK Commented Oct 19, 2010 at 8:12
- @Paniyar: that's not a duplicate. The issue in that question is due to the use of regular expressions to split a string - this question is about splitting a string by another string. – Andy E Commented Oct 19, 2010 at 8:14
- I cannot reproduce it: jsbin./imoki3 – Kobi Commented Oct 19, 2010 at 8:15
- works fine for me ( example ). How about adding the script tag, and better yet a link to a working page.. – Gabriele Petrioli Commented Oct 19, 2010 at 8:15
- @Kobi - really? I knew it! IE is getting personal with me! Still throwing an error when I try it... :-( – Ian Commented Oct 19, 2010 at 9:15
3 Answers
Reset to default 4The 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/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 pressed 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.
本文标签: IE won39t split string with JavascriptStack Overflow
版权声明:本文标题:IE won't split string with Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741698556a2393152.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论