Is there any way to combine all of this to reduce the amount of javascript?
$(document).ready(function() {
$(".individualImagebox img").bind("click", function()
{
var src = $(this).attr("src");
if (src.search(/Red\.jpg$/) >= 0) return;
// Removes the red overlay from the images folder
$('.individualImagebox img').attr( "src", function () {
var thisSRC = $(this).attr( "src");
return thisSRC.replace(/Red\.jpg$/, ".jpg");
});
// Adds the red overlay from the images folder
$(this).attr( "src", src.replace(/\.jpg$/, "Red.jpg") );
});
});
function ShowHide(index) {
var itemSelector = ".name:eq(" + index + ")";
$(".name .bio").fadeOut();
$(".name").not(itemSelector).fadeOut();
$(itemSelector).animate({"height": "show"}, { duration: 500 });
$(itemSelector + " .bio").animate({"height": "show"}, { duration: 500 });
}
KJYe.Name
17.2k5 gold badges51 silver badges63 bronze badges
asked Mar 29, 2011 at 14:59
Courtney Stephenson
9402 gold badges18 silver badges43 bronze badges
-
Is ".jpg" an image? If not, why not just remove the src attribute?bpierre– bpierre2011年03月29日 15:12:22 +00:00Commented Mar 29, 2011 at 15:12
2 Answers 2
$(function() { // $(function(){}) is a shortcut of $(document).ready(function(){})
var $activeImg; // Maintain a reference to the last activated img
$(".individualImagebox img").click(function(){
if (!!$activeImg) {
$activeImg.attr("src", function(i, src){
return src.replace(/(.+)Red\.jpg$/, "1ドル.jpg");
});
}
$activeImg = $(this).attr("src", function(i, src){ // replace attribute and updates active img reference
return src.replace(/(.+)\.jpg$/, "1ドルRed.jpg");
});
});
});
I don’t know exactly what you are trying to do but if possible, you should toggling a class instead of modifying the src attribute.
answered Mar 29, 2011 at 15:19
bpierre
11.6k2 gold badges28 silver badges28 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Courtney Stephenson
There are two files and one needs "red" at the end of them. For instance chuck.jpg and chuckRed.jpg
Combining methods won't save you space. Take a look at
answered Mar 29, 2011 at 15:04
Andre Pena
59.7k53 gold badges213 silver badges257 bronze badges
Comments
lang-js