作者pyLIC (PyBlues)
看板Web_Design
标题[问题] Javascript string处理 在IE不会动
时间Thu Oct 27 13:59:05 2011
功能: 输入 IP: 192.2.09.001 -> 192.2.9.1
错误状况: 192.2.09.001 -> undefinedundefinedundefined.undefined.undefined
undefined.undefinedundefinedundefined
正确环境: Chrome Firefox
错误环境: IE6 IE7
想请教为什麽在IE底下会这样 触犯了什麽
感谢
实作如下
function为ip_normalize(ip)
function remove_0(input)
{
var i;
var output_string="";
var tag=0;
for (i=0; i< input.length ; i++)
{
if(i==input.length-1 || tag==1)
{
output_string+= input[i];
}
else
{
if(input[i]!='0')
{
output_string+= input[i];
tag=1;
}
}
}
return output_string;
}
function ip_normalize(ip)
{
var ip_s = ip.split('.');
var ip_n = "";
var ip1="",ip2="",ip3="",ip4="";
if(ip_s.length!=4)
return ip;
ip1 = remove_0(ip_s[0]);
ip2 = remove_0(ip_s[1]);
ip3 = remove_0(ip_s[2]);
ip4 = remove_0(ip_s[3]);
ip_n = ip1 + '.' + ip2 + '.' + ip3 + '.' + ip4;
return ip_n;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.120.184.171
2F:→ pyLIC:楼上这连结有趣 虽然还找不到问题 只能说IE很XX 10/27 17:14
3F:推 maplenote:问题应该在於remove_0里面你用input[i]去呼叫 10/27 17:31
4F:→ maplenote:ie认定他为字串,不让你这样抓 可以用split("")强制切开 10/27 17:33
5F:→ maplenote:其实你也不用特别写个remove_0 用parseInt()就好了吧 10/27 17:36
6F:→ maplenote:好吧..我错了 测了一下用parseInt是不行的><" 10/27 17:42