JavaScript字符串替换IE问题

标签 javascript string internet-explorer-8 replace

这个简单的代码在 FF 和 Chrome 中运行良好...但在 IE8 中不行:

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

给予:

'undefined' is null or not an object error on line 2

我也尝试过这样的事情:

pathtop = pathtop.replace('', '');

同样的错误!

我在这个项目中使用 jQuery。

最佳答案

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

即兴猜测 - IE 忽略嵌入标签而支持 <object> ,因此 DOM 树中没有嵌入。并且您正在尝试获取不存在的 dom 对象的 src,从而使 pathtop 为 null,这意味着没有可用的替换方法。

关于JavaScript字符串替换IE问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7558279/

相关文章:

javascript - 如何停止asp :UpdatePanel async call from calling pageload

javascript - 脚本 CDN 使用、隐私和成本

c# - 如果在输出中使用 "+"或不使用 "+"有什么区别?

python - 在字符串中同时使用一个反斜杠和引号

javascript - IE8 正则表达式不起作用

javascript - 避免立即执行 Jquery 中的函数

javascript - 使用位置坐标值

iphone - 我试图将一个字符串从我的第一个 ViewController 传递到我的第二个 ViewController 但它返回 NULL

javascript - IE7+8 的 AJAX readystatechange 监听器

css - 具有最小高度和边框框的 IE8 布局