我对jQuery有点新鲜,只是想知道如何去传递字符串值而不是看起来是从选择器中引用jQuery项目?我很难解释,所以这里有一个示例演示。甚至不知道该如何标题,所以如果你能想到更好的标题,请编辑标题。jQuery - 另一个选择器内的选择器 - 传入变量失去它的值并成为索引器?
在我所在的线上$("td").filter(function(str){
传入的str成为TD所在的索引位置。所以在第一次调试时它是0,下一次是1等等。我试过谷歌,但我甚至不知道要搜索什么,任何文档/代码的帮助将不胜感激
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select[name='showTeam']").change(function() {
$("select[name='showTeam'] option:selected").each(function() {
var str = $(this).val().toLowerCase();
//str = what it was set to up there
//alert(str);
$("td").filter(function(str) {
//str = becomes a number = to position of TD.. ie for 5th TD match STR = 4 (starts at index 0)
return $(this).text().toLowerCase().indexOf(str) != -1;
}).css('background','red');
});
})
});
</script>
Show Team: <select id="showTeam" name="showTeam">
<option>All</option>
<option>Chelsea</option>
</select>
<div id="games">
<table border="1">
<tbody>
<tr>
<th>ID</th>
<th>Game date</th>
<th>Field</th>
<th>Home team</th>
<th>Home team score</th>
<th>Away team</th>
<th>Away team score</th>
<th>Game type</th>
</tr>
<tr class="odd_line" id="game_460">
<td>459</td>
<td>03 Nov 19:00</td>
<td>Field 2</td>
<td>Madrid </td>
<td>3</td>
<td>Bayern Munich </td>
<td>1</td>
<td>Season</td>
</tr>
<tr class="odd_line" id="game_461">
<td>460</td>
<td>03 Nov 19:00</td>
<td>Field 3</td>
<td>chelsea</td>
<td>5</td>
<td>arsenal</td>
<td>4</td>
<td>Season</td>
</tr>
</div>
[有关'.filter()']的文档(http://api.jquery.com/filter/) – bfavaretto 2012-02-16 19:18:17
呃我不知道我是如何错过的,我想我必须重新思考我的方法..谢谢 – 2012-02-16 19:20:23
你在做什么?你打算用'filter()'方法实现什么? – 2012-02-16 19:20:51