Home >Web Front-end >Front-end Q&A >How to add a style attribute in jquery
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.
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.
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.
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 .
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.
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.
Open the test.html file in the browser, click the button to see the effect.
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!