replace()用於在字符串中用一些字符替換另一些字符,或替換一個與正則表達式匹配的子串。
lastIndexOf()方法從指定的字符串從後向前搜索,不包括選中的字符串,從1開始計數。
substring()用於提取字符串中介於兩個指點下標之間的字符,包括兩個字符串本身。
實例:
//衣服顏色切換 var $color_img=$(".color_change ul li img"); $color_img.bind("click",function(){ $(this).addClass("hover").parent().siblings().find("img").removeClass("hover"); var colorSrc=$(this).attr("src"); var colori=colorSrc.lastIndexOf("."); var colorUnit=colorSrc.substring(colori); colorcolorSrc.substring(0,colori); var colorSrc_big=colorSrc+"_one"+"_big"+colorUnit; var colorSrc_small=colorSrc+"_one"+"_small"+colorUnit; $("#bigImg").attr({"src":colorSrc_small}); $("#thickImg").attr("href",colorSrc_big); var alt=$(this).attr("alt"); $(".color_change strong").text(alt); var newColor colorSrc.substring(15,colori) //用substring()方法在colorSrc中找到相應的顏色單詞 //var newColorcolorSrc.replace("p_w_picpaths /pro_img/",""); //replace()方法是用於在字符串中用一些字符替換另一些字符,或替換一個與正則表達式匹配的子串。 //replace()在原文件可以用,不知道怎麼到我這裏就是不行,因此用sbustring()代替了。 $(".imgList li").hide(); $(".imgList").find(".imgList_"+newColorSrc).show(); //用於解決bug,切換顏色後,放大圖片還是顯示原來的圖片。 $(".imgList").find(".imgList_"+newColorSrc).eq(0).find("a").click(); }).hover(function(){ $(this).addClass("hover1"); },function(){ $(this).removeClass("hover1"); });