JavaScript randomly adds a picture from the first Dom to the second Div example _javascript tips

Source: Internet
Author: User
JavaScript randomly adds a picture from the first Dom to the second Div, which is a simple example of five images in the first div that extract the random two to display into the second Div.
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> randomly show five pictures inside two pictures-test picture with Baidu space picture </title>
<script type= "Text/javascript" >
Window.onload = function () {
var count=4;
var original=new array;//Original Array
var tpdiv = document.getElementById (' Kstupian '); Get DOM Objects
var images = tpdiv.getelementsbytagname ("img");
for (Var i=0;i<count;i++) {original[i]=i}
Original.sort (function () {return 0.5-math.random ();});
var mydiv = document.getElementById (' mydiv '); Get DOM Objects
for (Var i=0;i<2;i++) {
var bigimg = document.createelement ("img"); Create an IMG element
Bigimg.width= "200"; 200 pixels without px
Bigimg.src=images.item (Original[i]). src; Assign values to the SRC attribute of an IMG element
Mydiv.appendchild (BIGIMG); Adding child elements to the DOM img
}

};
</script>

<body>
<div id= "Kstupian" >
</div>
<div id= "Mydiv" ></div>
</body>
Related Article

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.