JQuery:image src变量清理

时间:2012-04-30 02:13:17

标签: javascript jquery jquery-ui

过去几天我一直在教自己如何使用jquery,没有多少使用它,我仍然有点陷入旧方式(回到10年前lol)开始我从下载了JQuery桌面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);

没有运气,感谢任何时间的信息

2 个答案:

答案 0 :(得分:2)

尝试:

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

答案 1 :(得分:1)

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

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

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

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