Replace (), lastIndexOf (), substring ()

Source: Internet
Author: User

Replace () is used to replace other characters with some characters in a string, or replace a substring that matches a regular expression.


The lastIndexOf () method searches from the back and forward of the specified string, excluding the selected string and starts counting from 1.


Substring () is used to extract characters that are intermediary between two pointer subscripts, including the two strings themselves.


Instance:

// Clothes color switch 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 + "_ o Ne "+" _ 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) // use the substring () method to find the corresponding color word in colorSrc // var newColorcolorSrc. replace ("images/pro_img/", ""); // The replace () method is used to replace other characters with some characters in the string, or replace a substring that matches the regular expression. // Replace () can be used in the original file. I don't know how to use it here, so it is replaced by sbustring. $ (". ImgList li "). hide (); $ (". imgList "). find (". imgList _ "+ newColorSrc ). show (); // used to solve the bug. After switching the color, enlarge the image and display the original image. $ (". ImgList "). find (". imgList _ "+ newColorSrc ). eq (0 ). find (""). click ();}). hover (function () {$ (this ). addClass ("hover1") ;}, function () {$ (this ). removeClass ("hover1 ");});


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.