Related knowledge: JavaScript

Source: Internet
Author: User

Related knowledge: JavaScript

JavaScript is a programming language that is widely used for interactive effects in Web sites and applications.

JavaScript can interact with styles, and you can write programs to dynamically change the style of elements on a document.

There are three ways to achieve this:

    • Control style Sheets-you can add, delete, and modify style sheets.
    • Control style rules-you can add, delete, and modify style rules.
    • Controls a single element in the DOM-you can modify the element style without relying on the style sheet.
More details
To learn more about JavaScript, you can go to this wiki JavaScript.
Example: an instance of JavaScript

Create a newdoc5.html的页面,把下面的代码复制粘贴进入,注意要保证保存了所有的代码:

<! DOCTYPE html>>><title>mozilla CSS Getting started-javascript Demonstration</title><linkRel="Stylesheet"Type="Text/css"Href="Style5.css"/><scriptType="Text/javascript"Src="Script5.js"></script>><body>>javascript sample><divId= "Square "> </div><button>click me</button ></body>           

To create a new CSS file style5.css , copy and paste the following style code into the file:

#square { width: 20em; height: 20em; border: 2px inset gray; margin-bottom: 1em; } button { padding: .5em 2em; }

Create a new JavaScript file script5.js , copy and paste the following code into the file:

JavaScript Demonstrationvar changebg=function(Event){Console.Log("Method called");var me= Event. Target, Square= document.getElementById("Square"); Square. style. backgroundcolor="#ffaa44"; Me.SetAttribute("Disabled","Disabled");SetTimeout(function(){Cleardemo(Me)},2000);}functionCleardemo(Button){var square= document.getElementById("Square"); Square. style. backgroundcolor="Transparent"; button.RemoveAttribute("Disabled";} var button = document.< Span class= "token function" >queryselector (;button.addeventlistener ( "click" ;console< Span class= "token punctuation". log (Button             

Open the HTML file with your browser and click the button.

Here's an online example: Here's the Live Example

JavaScript Demonstration

JavaScript Demonstration

Important Notes
    • HTML documents are linked to CSS files and JavaScript files.
    • The script modifies the style of the DOM element directly and changes the style of the button by modifying the value of the property.
    • document.getElementById("square")functionally with CSS selectors in JavaScript#square的功能是类似的。
    • In JavaScript code, backgroundColor corresponds to a CSS property background-color . Because JavaScript is not allowed to appear in the name of the underlined, so the use of camel-style, the wording to do instead.
    • The browser has built-in CSS rules for the button button[disabled="true"] , which causes the buttons to appear in a non-clickable style that is not the same as expected.
Challenge
Modify the script code to achieve the following effect: When the color changes, let the block jump to the right 20em distance, and then back to the original position.

Here's a solution example: see a solution to this challenge.

Related knowledge: JavaScript

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.