JS small instance (click the link to change the image)

Source: Internet
Author: User

  1. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  2. <Head>
  3. <Meta http-equiv = "Content-Type" content = "text/html; charset = GBK"/>
  4. <Title> image gallery </title>
  5. <SCRIPT type = "text/JavaScript">
  6. Function showpic (whichpic ){
  7. VaR source = whichpic. getattribute ("href"); // gets the href value of the attribute of the currently clicked element.
  8. VaR placeholder = Document. getelementbyid ("Placeholder"); // obtain the target
  9. Placeholder. setattribute ("src", source );
  10. /*
  11. Set the target property SRC. To change the image.
  12. Setattribute completes two operations: Create an attribute first and assign a value. If an attribute exists, the attribute value is overwritten.
  13. Of course we can use placeholder. src = source;
  14. However, we recommend that you use setattribute (), which is Level 1 Dom,
  15. It can modify any attribute of any element in the document.
  16. In addition, it has better portability.
  17. */
  18. VaR text = whichpic. getattribute ("title ");
  19. // Obtain the attribute title value of the currently clicked Element
  20. VaR description = Document. getelementbyid ("Description"); // obtain the target
  21. Description. firstchild. nodevalue = text;
  22. /*
  23. Or use
  24. Description. childnodes [0]. nodevalue = text;
  25. */
  26. }
  27. </SCRIPT>
  28. </Head>
  29. <Body>
  30. <H1> JavaScript Image Library
  31. <! --
  32. Add events to the tag to avoid redirection.
  33. Solution: The function returns false. The event indicates that the link is not clicked.
  34. Of course, this is when the href value is useful.
  35. If the href value is javascript: void (0.
  36. -->
  37. <Ul>
  38. <Li>
  39. <A href = "images/1.gif" Title =" test1 "onclick =" showpic (this); Return false; "> test1 </a>
  40. </LI>
  41. <Li>
  42. <A href = "images/2.gif" Title =" Test2 "onclick =" showpic (this); Return false; "> Test2 </a>
  43. </LI>
  44. <Li>
  45. <A href = "images/3.gif" Title =" test3 "onclick =" showpic (this); Return false; "> test3 </a>
  46. </LI>
  47. <Li>
  48. <A href = "images/4.gif" Title =" test4 "onclick =" showpic (this); Return false; "> test4 </a>
  49. </LI>
  50. <Li> <a href = "javascript: void (0);" Title = "test5"> test5 </a>
  51. </LI>
  52. </Ul>
  53. <P id = "Description"> select image. </P>
  54. </Body>
  55. </Html>

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.