0

过去几天我一直在自学如何使用 jquery,并没有使用太多,我仍然坚持旧方式(回到 10 年前哈哈)开始我从 JQuery Desktop 下载了 JQuery Desktop - Nathan Smith,为了避免重复使用图像 src 链接不可避免地使文件大于它需要的大小。因此,在使参考变量基本上开始再次使用相同的东西时...我尝试查看如何压缩它或清理它,但我一直在尝试做的事情陷入死胡同...如果有人碰巧知道我能做什么,那会很棒。

代码

enter code here
            /*  SAVES DATA SPACE ALSO CREATES QUICK REFFERENCE/VARIABLE */
            var healthVar= 'assets/img/icons/health.png';
            var emailVar = 'assets/img/icons/email.png';
            var linkVar = 'assets/img/icons/link.png';
            var noteVar = 'assets/img/icons/note2.png';
            var videoVar = 'assets/img/icons/video1.png';
            var xxxVar = 'assets/img/icons/xxx.png';
            var socialVar = 'assets/img/icons/social.png';
            var webdesktopVar = 'assets/img/icons/webdesktop.png';
            var androidVar = 'assets/img/icons/android.png';
            var devVar = 'assets/img/icons/development.png';
            var secureVar = 'assets/img/icons/secure.png';
            var signalVar = 'assets/img/icons/signal.png';
            var setVar = 'assets/img/icons/settings.png';
            var spamVar = 'assets/img/icons/spam.png';
            var feedbackVar = 'assets/img/icons/feedback.png';

            $("#health_Pic").attr("src", healthVar);$("#health_PicB").attr("src", healthVar);$("#health_PicC").attr("src", healthVar);
            $("#email_Pic").attr("src", emailVar);$("#email_PicB").attr("src", emailVar);$("#email_PicC").attr("src", emailVar);
            $("#link_Pic").attr("src", linkVar);$("#link_PicB").attr("src", linkVar);$("#link_PicC").attr("src", linkVar);
            $("#note_Pic").attr("src", noteVar);$("#note_PicB").attr("src", noteVar);$("#note_PicC").attr("src", noteVar);
            $("#video_Pic").attr("src", videoVar);$("#video_PicB").attr("src", videoVar);$("#video_PicC").attr("src", videoVar);
            $("#social_Pic").attr("src",socialVar);$("#social_PicB").attr("src", socialVar);$("#social_PicC").attr("src", socialVar);
            $("#webdesktop_Pic").attr("src",webdesktopVar);$("#webdesktop_PicB").attr("src",webdesktopVar);$("#webdesktop_PicC").attr("src",webdesktopVar);
            $("#android_Pic").attr("src",androidVar);$("#android_PicB").attr("src",androidVar);$("#android_PicC").attr("src",androidVar);
            $("#dev_Pic").attr("src",devVar);$("#dev_PicB").attr("src",devVar);$("#dev_PicC").attr("src",devVar);
            $("#secure_Pic").attr("src",secureVar);$("#secure_PicB").attr("src", secureVar);$("#secure_PicC").attr("src", secureVar);
            $("#signal_Pic").attr("src", signalVar);$("#signal_PicB").attr("src", signalVar);$("#signal_PicC").attr("src", signalVar);
            $("#settings_Pic").attr("src", setVar);$("#settings_PicB").attr("src", setVar);$("#settings_PicC").attr("src", setVar);
            $("#spam_Pic").attr("src",spamVar);$("#spam_PicB").attr("src", spamVar);$("#spam_PicC").attr("src", spamVar);
            $("#feedback_Pic").attr("src", feedbackVar);$("#feedback_PicB").attr("src", feedbackVar);$("#feedback_PicC").attr("src", feedbackVar)

我试图这样做:

$("#health_Pic","#health_PicB","#health_PicC").attr("src", healthVar);

运气不好,感谢您提供任何信息

4

2 回答 2

2

尝试:

$("#health_Pic,#health_PicB,#health_PicC").attr("src", healthVar);
于 2012-04-30T02:26:41.120 回答
1

jQuery attr 函数只接受选择器获得的第一个元素。

Get the value of an attribute for the 
first element in the set of matched elements.

使用循环更改多个图像 src。

编辑:在检查文档并自己尝试之后,我发现使用 .attr 作为设置器确实为所有元素设置了值。感谢 j08691 指出这一点。

于 2012-04-30T02:23:07.300 回答