错误:未捕获的异常:[exception.."字符串包含一个..quot;

Error: uncaught exception: [Exception..."String contains an...."

本文关键字:quot 包含一 exception 字符串 异常 错误      更新时间:2023-09-26

我正在运行以下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 + "%");
        }
});

我得到以下错误:

错误:未捕获的异常:[exception…"字符串包含无效的character"code:"5"nsresult:"0x80530005(NS_ERROR_DOM_INVALID_CHARACTER_ERR)"位置:"http://code.jquery.com/jquery-1.6.4.min.js线路:2"]

实际上,我很难找到这个问题,因为它不是很能描述问题或问题的根源。有人能解释一下这个问题吗?

感谢

您似乎不知道正确的语法。试试这个:

$('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 + "px");
        }
});

$(''img'')不起作用。应为$('img')。还有一些其他地方你试图逃避单引号,但你不需要。你的代码应该更像这样:

$('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 + "px");
        }
});

看起来它运行非常适合我——尽管它不会改变图像的大小——所以问题可能在代码/页面的其他地方。

然而,关于代码中的逻辑,我不确定您在这里要做什么;

parseInt((W/666)*100)

你想让所有的图片都达到最大宽度吗?即缩小浏览器中的图片?如果是这样,为什么不将大小设置为固定值,比如;

$('img').each(
    function(){
        var maxwidth = 300;
        var w = parseInt(this.width);
        var h = parseInt(this.height)
        if(w > maxwidth){
            var hscale = parseInt(h/(w/maxwidth));
            $(this).width(maxwidth).height(hscale);
        }
});