JS Instance 5

Source: Internet
Author: User

Do such an effect after the mouse click a name color, unchecked mouse move up color

First layout This effect, then add click event onclick to each table, mouse over event onmousemove, mouse out event onmouseout

It's easy to confuse these events.

    

Because it is the class name all onclick= "Dian (This)" Onmousemove= "Fang (This)" has a this, meaning to click on itself, because the class names, plus this know which one is clicked

First click the event's function

    <script type= "Text/javascript" >function Dian (a) {var xiao=document.getelementsbyclassname ("Xiao") for (Var i=0; i<xiao.length;i++) {xiao[i].style.backgroundcolor= "#00F"}a.style.backgroundcolor= "Red";}

Click Turn red, and then the loop without clicking turns into the original color

Next write the mouse and put on the color

                function Fang (b) {var xiao=document.getelementsbyclassname ("Xiao") for (var i=0; i<xiao.length;i++) {xiao[i]. Style.backgroundcolor= "#00F"}b.style.backgroundcolor= "Red";}

So look is no problem, the actual effect is only mouse on the color effect, the mouse click effect is not. This is because the priority relationship

So to add a virtual property, the mouse click after the addition of properties, no click to move out of this property

                function Dian (a) {var xiao=document.getelementsbyclassname ("Xiao") for (var i=0; i<xiao.length;i++) {xiao[i]. RemoveAttribute ("BS")//move out of this attribute xiao[i].style.backgroundcolor= "#00F"}a.setattribute ("BS", 1)//without clicking on it, or simply adding an attribute, and attribute value a.style.backgroundcolor= "Red";} function Fang (b) {var xiao=document.getelementsbyclassname ("Xiao") for (var i=0; i<xiao.length;i++) {if (Xiao[i]. GetAttribute ("BS")! = "1")//According to the above set, with if judge, as long as the property is not equal to 1 is the original color,
If there is no such condition, the Click event does not exist {xiao[i].style.backgroundcolor= "#00F"}}b.style.backgroundcolor= "Red";}

This click Color and not click on the mouse to color, but the mouse moved out after the color is also to continue to write the mouse out of the function, the same is, with if the added property is not equal to a change back to the original color, the purpose is to ensure that the mouse out of the time, click the selected will not be changed by the mouse color. Because the mouse click adds a property Bs=1, the removal of discoloration is a condition where BS is not equal to 1.

                function Zou () {var xiao=document.getelementsbyclassname ("Xiao") for (var i=0; i<xiao.length;i++) {if (Xiao[i]. GetAttribute ("BS")! = "1")//if there is no such condition, the effect of the click does not exist. {xiao[i].style.backgroundcolor= "#00F"}}}    </script>

The specific effect is like the computer QQ friend selected a friend, the rest of the Friends mouse movement and color changes

JS Instance 5

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.