简体   繁体   English

IE不会用Javascript分割字符串

[英]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, however in IE (7 and 8) it throws an error on the split-function (this property or method is not supported by this object). 这在FF,safari,Chrome中运行良好,但是在IE(7和8)中它会在分割函数上抛出错误(此对象不支持此属性或方法)。

if I alert the 'id'-variable I get something like plus_5751 . 如果我提醒“id”变量,我会得到类似plus_5751东西。 (so I want to get the '5751' part) if I do alert(typeof(id)) I get String as an answer... (所以我想得到'5751'部分)如果我做alert(typeof(id))我得到String作为答案...

Maybe somebody can point me to the right answer? 也许有人可以指出我正确的答案?

Thx 谢谢

The split works pretty well in IE. split在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. 它是一个对象,所有输入字段都具有articlenr的名称,因此当您尝试为其分配字符串时,IE将退出并且“此对象不支持此属性或方法”。

Your code works just fine for me in Internet Explorer - as it should be expected to. 您的代码在Internet Explorer中对我来说效果很好 - 正如它应该的那样。 The problem must lie elsewhere, perhaps something is overriding String.prototype.split ?. 问题必须在其他地方,也许某些事情会覆盖String.prototype.split You can see a working example of your code at http://jsfiddle.net/AndyE/6K77Y/ . 您可以在http://jsfiddle.net/AndyE/6K77Y/上看到代码的工作示例。 The first thing to check for is any Internet Explorer specific code in your scripts. 要检查的第一件事是脚本中的任何Internet Explorer特定代码。

I would make one small improvement for efficiency. 我会为效率做一个小改进。 $(this).attr('id'); is pretty much the long winded way of writing this.id . this.id文章几乎是漫长的啰嗦方式。 It's slower, because a new jQuery object has to be created and then the attr function has to run. 它的速度较慢,因为必须创建一个新的jQuery对象,然后运行attr函数。 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. 尝试将变量'id'重命名为其他内容。 IE doesn't like it when you name things in your scripts the same as items in the DOM. 当您在脚本中命名与DOM中的项目相同时,IE不喜欢它。

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特定错误的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM