dev
jquery filter()
dev_0hoon
2024. 6. 13. 23:30
<script>
$(document).ready(function () {
$('td').filter(function (index, selector) {
if (index % 2 == 0) {
$(selector).css('background-color', 'red');
}
});
});
</script>
필터는 셀렉터를 가져올 수 있음, 순서에 따라 조건을 넣어서 사용 할 수 있다.
필터 메서드의 메서드 체이닝(Chaining)
여러 필터 메서드를 사용하는데에 있어서의 장점은 메서드 체이닝을 지원한다는 사실입니다. 다음과 같이 css() 메서드를 사용하고 나서 jQuery dom 객체를 반환하므로 연달아서 필터 메서드를 사용할 수 있는 장점이 있고, 코드를 훨씬 간결하게 작성할 수 있습니다.
|
<script>
$(document).ready(function () {
$('td')
.css('background-color', 'red')
.eq(3)
.css('background-color', 'green');
});
</script>
|
출처: https://dololak.tistory.com/416 [코끼리를 냉장고에 넣는 방법:티스토리]
|
<script>
$(document).ready(function () {
var firstValue = $('td').first().text();
var lastValue = $('td').last().text();
console.log('first() = ' + firstValue);
console.log('last() = ' + lastValue);
});
</script>
|
출처: https://dololak.tistory.com/416 [코끼리를 냉장고에 넣는 방법:티스토리]