In HTML and CSS, we want to set the color of a button. Although we can achieve the desired effect, the process is rather cumbersome. We can easily accomplish this task using jQuery. Suppose now we have a set of buttons When we click one of them, it turns pink, and when we click another one, all the buttons except the one that was clicked return to their original colors. What should we do? In fact, it only takes a few lines of code to complete <script> $(function(){ $('Button').click(function(){ //First set the clicked button color to pink$(this).css('background','pink'); // Set the color of other sibling elements except the current element to "empty" //siblings is all the sibling elements of the selected current element (excluding the current element) $(this).siblings('button').css('background',''); }); }); </script> The effect is as follows: Here is the complete code: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>Wellfancy</title> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <style> div{ border: 2px solid lightpink; margin: 50px auto; padding:50px; } </style> </head> <body> <div> <button>Option 1</button> <button>Option 2</button> <button>Option 3</button> <button>Option 4</button> <button>Option 5</button> <button>Option 6</button> <button>Option 7</button> </div> <script> $(function(){ $('Button').click(function(){ $(this).css('background','pink'); $(this).siblings('button').css('background',''); }); }); </script> </body> </html> As you can see, isn’t it much simpler when we use jQuery than just using CSS? We can use shorter codes to complete more complex operations. You may also be interested in:
|
<<: mysql8.0.11 winx64 installation and configuration tutorial
>>: Screen command and usage in Linux
Let me first explain why the text is not vertical...
The load is generally estimated during system des...
In the previous article, we introduced how to use...
When writing the HTTP module of nginx, it is nece...
Table of contents 1. Merge interface 1.1 Non-func...
This article is intended to be a starting point f...
Description: Limit the number of lines of text di...
Due to business needs, there are often rush purch...
Preface Recently, I took over a client's nati...
1. Mobile selection of form text input: In the te...
Optgroup is used in the select tag to make the dro...
Virtualization 1. Environment Centos7.3 Disable s...
1. Unzip nginx-1.8.1.tar.gz 2. Unzip fastdfs-ngin...
Preface: Mybatis special character processing, pr...
1. Problems encountered In the process of distrib...