是否有一种方法可以使用boolean parameter根据元素是否具有特定类来切换集合的元素?我尝试了以下方法,但是我认为$(this)
与".person"
无关。
// Would show the entries with the selected country, hide the others
function toggleCountries(country){
$(".person").toggle( $(this).hasClass(country) );
}
DOM:
...
<select onchange="toggleCountries($(this).val())">
<option value="usa">USA</option>
<option value="canada">Canada</option>
...
</select>
...
<li class="person usa"></li>
<li class="person canada"></li>
<li class="person canada"></li>
<li class="person mexico"></li>
...
注:我知道如何使用多行切换元素,但我很好奇是否有一种一行的方法可以用于将来参考,可能会有用。