JavaScript字符串替换IE问题

发布于 2024-12-06 17:22:25 字数 388 浏览 1 评论 0原文

这个简单的代码在 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯叶蝶 2024-12-13 17:22:25

IE 上的 pathtop 很可能为 null,因为 jquery find/attr 链失败。将其分成几部分,并找出哪一层($('#autoplay')、.find().attr() 返回 null。

临时猜测 - IE 会忽略嵌入标签而使用 ,因此 DOM 树中没有嵌入,并且您试图获取不存在的 dom 对象的 src,将 pathtop 设为 null,这意味着没有可用的替换方法。

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.

旧梦荧光笔 2024-12-13 17:22:25

尝试

var pathtop = $('#autoplay').find('object').attr('src');
pathtop = pathtop.replace('http://www.youtube.com/v/', '');

try

var pathtop = $('#autoplay').find('object').attr('src');
pathtop = pathtop.replace('http://www.youtube.com/v/', '');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文