Home  >  Article  >  Web Front-end  >  How to add a style attribute in jquery

How to add a style attribute in jquery

藏色散人
藏色散人Original
2021-11-17 11:07:229466browse

How to add a style attribute in jquery: 1. Create a new html file; 2. Use the p tag to create a line of text; 3. Use the button tag to create a button; 4. Get the p tag object through the id; 5. Just use the css() method to add styles to style.

How to add a style attribute in jquery

The operating environment of this article: Windows 7 system, jquery version 3.2.1, DELL G3 computer

How to add a style attribute to jquery?

jquery adds attributes to style:

Create a new html file named test.html to explain how jquery adds attributes to style.

How to add a style attribute in jquery

In the test.html file, use the p tag to create a line of text, and set its id to mypp, which is used to obtain the p tag object through this id below.

How to add a style attribute in jquery

In the test.html file, in the p tag, use the style attribute to set the background color of the p tag text to black and the text color to white .

How to add a style attribute in jquery

In the test.html file, use the button tag to create a button and bind the onclick click event to the button button. When the button is clicked, Execute the addcss() function.

How to add a style attribute in jquery

In the js tag, create the addcss() function. Within the function, obtain the p tag object through the id, use The css() method adds style attributes to style. For example, add the font-size attribute and set it to 30px, which means the text size is 30px.

How to add a style attribute in jquery

Open the test.html file in the browser, click the button to see the effect.

How to add a style attribute in jquery

Recommended learning: "jquery video tutorial"

The above is the detailed content of How to add a style attribute in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn