错误:未捕获的异常:[异常...“字符串包含...”
我正在运行以下 javascript:
$('img').each(
function(){
var w = parseInt(this.width);
alert(w);
alert(this.src);
if(w > 300){
var percent = parseInt((w/666)*100);
$(this).removeAttr('width height').css('width',percent + "%");
}
});
并且收到以下错误:
错误:未捕获的异常:[异常...“字符串包含无效的 字符”代码:“5”nsresult:“0x80530005 (NS_ERROR_DOM_INVALID_CHARACTER_ERR)”位置: “http://code.jquery.com/jquery-1.6.4.min.js 行:2”]
我在尝试追踪这个问题时遇到了麻烦,因为它不能很好地描述问题或来源的问题。任何人都可以阐明这个问题吗?
谢谢
I am running the following javascript:
$('img').each(
function(){
var w = parseInt(this.width);
alert(w);
alert(this.src);
if(w > 300){
var percent = parseInt((w/666)*100);
$(this).removeAttr('width height').css('width',percent + "%");
}
});
And I am getting the following error:
Error: uncaught exception: [Exception... "String contains an invalid
character" code: "5" nsresult: "0x80530005
(NS_ERROR_DOM_INVALID_CHARACTER_ERR)" location:
"http://code.jquery.com/jquery-1.6.4.min.js Line: 2"]
I'm having trouble actually trying to track this one down as it isn't very descriptive of the problem or the source of the problem. Can anyone shed some light on the problem?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您不知道正确的语法。
试试这个:
It looks like you don't know the proper syntax.
Try this:
$(\'img\')
不起作用。应为$('img')
。还有其他一些地方您试图转义单引号,但您不需要这样做。您的代码应该看起来更像这样:$(\'img\')
won't work. Should be$('img')
. There's a few other places where you are trying to escape the single quotes, but you don't need to. Your code should look more like this:看起来它对我来说运行完美 - 尽管它没有改变图像的大小——所以问题可能出在代码/页面的其他地方。
然而,关于代码中的逻辑,我不确定您在这里想要做什么;
您是否想让所有图片都达到最大宽度?即在浏览器中缩小图片的尺寸? 如果是这样,为什么不将大小设置为固定值,例如;
Looks like it is running perfectly for me -- although it does not change the size the images -- so the problem is probably somewhere else in your code/page.
On the logic in the code, i am however unsure about what you are trying to do here;
Are you trying to get all the pictures to be a width max size? i.e. downscaling the picture in the browser? If so, why not just set the size to a fixed value, like;