Home >Web Front-end >JS Tutorial >Detailed explanation of basic usage examples of filters in jQuery

Detailed explanation of basic usage examples of filters in jQuery

小云云
小云云Original
2018-01-25 10:41:241031browse

This article mainly introduces the basic usage of filters in jQuery, and analyzes the related operating techniques of jQuery filters for judging and setting table element attributes in the form of simple examples. Friends who need it can refer to it. I hope it can help everyone. .

HTML text:


##

<input type="button" id="b1" value="偶数行红色"><br>
<input type="button" id="b2" value="奇数行绿色"><br>
<table border=1 width="100px">
<tr><td>1</td><td>1</td><td>1</td><td>1</td></tr>
<tr><td>2</td><td>2</td><td>2</td><td>2</td></tr>
<tr><td>3</td><td>3</td><td>3</td><td>3</td></tr>
<tr><td>4</td><td>4</td><td>4</td><td>4</td></tr>
<tr><td>5</td><td>5</td><td>5</td><td>5</td></tr>
</table><br>
<input type="button" id="b3" value="奇数列红色"><br>
<input type="button" id="b4" value="偶数列绿色"><br>
<table border=1 width="100px">
<tr><td>1</td><td>1</td><td>1</td><td>1</td></tr>
<tr><td>2</td><td>2</td><td>2</td><td>2</td></tr>
<tr><td>3</td><td>3</td><td>3</td><td>3</td></tr>
<tr><td>4</td><td>4</td><td>4</td><td>4</td></tr>
<tr><td>5</td><td>5</td><td>5</td><td>5</td></tr>
</table><br>
<input type="button" id="b5" value="奇数元素绿色"><br>
<table border=1 width="100px">
<tr><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td></tr>
<tr><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td></tr>
<tr><td>3</td><td>3</td><td>3</td><td>3</td><td>3</td></tr>
<tr><td>4</td><td>4</td><td>4</td><td>4</td><td>4</td></tr>
<tr><td>5</td><td>5</td><td>5</td><td>5</td><td>5</td></tr>
</table>

Javascript operation code:


$(&#39;#b1&#39;).click(function(){
/*table:eq(0) tr:even table:eq(0):筛选出第一张表格 tr:even:筛选出tr属性的对象
*注意table和tr对象中间有空格,表示从属关系
*/
$(&#39;table:eq(0) tr:even&#39;).css("background","red"); 
});
$(&#39;#b2&#39;).click(function(){
$(&#39;table:eq(0) tr:odd&#39;).css("background","green");
});
$(&#39;#b3&#39;).click(function(){
$(&#39;table:eq(1) td:even&#39;).css("background","red");
});
$(&#39;#b4&#39;).click(function(){
$(&#39;table:eq(1) td:odd&#39;).css("background","green");
});
$(&#39;#b5&#39;).click(function(){
$(&#39;table:eq(2) td:even&#39;).css("background","green");
});

Effect:

Related recommendations:

Detailed explanation of how to use VUE2.X filter

Detailed examples of the basic usage of filters in jQuery

JQuery, selector/filter/performance optimization


The above is the detailed content of Detailed explanation of basic usage examples of filters 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