错误:未捕获的异常:[异常...“字符串包含...”

发布于 2025-01-08 09:39:16 字数 614 浏览 1 评论 0原文

我正在运行以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(3

泪痕残 2025-01-15 09:39:16

看来您不知道正确的语法。
试试这个:

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

        }
});

It looks like you don't know the proper syntax.
Try this:

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

        }
});
困倦 2025-01-15 09:39:16

$(\'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");

        }
});

$(\'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:

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

        }
});
缺⑴份安定 2025-01-15 09:39:16

看起来它对我来说运行完美 - 尽管它没有改变图像的大小——所以问题可能出在代码/页面的其他地方。

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

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);
        }
});

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;

parseInt((W/666)*100)

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;

$('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);
        }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文