JavaScript字符串替换IE问题
这个简单的代码在 FF 和 Chrome 中运行良好...但在 IE8 中不起作用:
var pathtop = $('#autoplay').find('embed').attr('src');
pathtop = pathtop.replace('http://www.youtube.com/v/', '');
给出:
第 2 行的“未定义”为 null 或不是对象错误
我也尝试过类似的操作:
pathtop = pathtop.replace('', '');
同样的错误!
我在这个项目中使用 jQuery。
This simple code works fine in FF and Chrome... but not in IE8:
var pathtop = $('#autoplay').find('embed').attr('src');
pathtop = pathtop.replace('http://www.youtube.com/v/', '');
Gives:
'undefined' is null or not an object error on line 2
I also tried something like this:
pathtop = pathtop.replace('', '');
and the same error!
I am using jQuery in this project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IE 上的 pathtop 很可能为 null,因为 jquery find/attr 链失败。将其分成几部分,并找出哪一层($('#autoplay')、
.find()
或.attr()
返回 null。临时猜测 - IE 会忽略嵌入标签而使用
pathtop on IE is most likely null, because the jquery find/attr chain failed. Split it up into parts and find out which layer ($('#autoplay'),
.find()
or.attr()
is returning a null.Offhand guess - IE's ignoring embed tags in favor of
<object>
, so there's no embed in the DOM tree. and you're trying to get the src of a non-existence dom object, making pathtop null, which means there's no replace method available for it.尝试
try