如何解决jQuery Length方法和IE7
|if (($(this)[0].value[$(this)[0].value.length - 1] == \'A\' || $(this)[0].value[$(this)[0].value.length - 1] == \'P\') && collFormat == 18) {
$(this)[0].value = $(this)[0].value + \'M\';
}
我有一个jquery脚本将\'M \'附加到时间字符串中,例如:\'xxx A \'到\'xxx AM \'。该脚本可在IE8,IE9,Firefox中运行,但不能在兼容模式和IE 7中运行。$(this)[0] .value [0]在IE7和兼容模式下的浏览器中未定义。请提供替代解决方案。
预先感谢。
解决方法
您无法在IE中使用
[n]
从字符串中获取单个字符。
相反,您应该致电charAt
:
this.value.charAt(this.value.length - 1)
,我认为您正在寻找这样的东西:
$(this).val(function(i,oldVal) { // set the element\'s value to the return value of this function
var lastChar = oldVal.substr(-1); // get the last character of the current value
return oldVal + // have the original value with something added
(
(lastChar == \'A\' || lastChar == \'P\') && collFormat == 18) ? // if this is the case
\'M\' : // add an M
\'\' // otherwise,add an empty string
);
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。