이것은 모두 .back
, .next
및 .tag
요소에 특정 동작을주는 것에 관한 것입니다. 다음과 같이
조직의 코드를 유지하기 위해, 그것은 편리하고 재사용, 사용자 지정 이벤트 핸들러, 단지 이벤트 핸들러가 포함에 모든 것을 수행하는 것이 유리입니다 :
- 는 'findPrev'이벤트 핸들러가 이전 태그를 찾을 수 세트에서 다음 세트를 찾으려면 'findNext'이벤트 핸들러가
- 입니다.
당신이에서
Back
및
Next
버튼을 활성화/비활성화 할 몇 가지 여분의 줄을 추가 비교적 사소한 보너스로, 그 둘레에 당신의 마음을했으면
$(document).ready(function() {
$(".nav .back").on('click', function(e) {
e.preventDefault();
if(this.href) { $(".wrapper .active").triggerHandler('findPrev').click(); }
});
$(".nav .next").on('click', function(e) {
e.preventDefault();
if(this.href) { $(".wrapper .active").triggerHandler('findNext').click(); }
});
$(".tag").on('findPrev', function() { // <<< custom event handler
var $tags = $(this).closest('.wrapper').find('.tag');
var index = $tags.index(this);
return (index > 0) ? $tags.eq(index - 1) : $();
}).on('findNext', function() { // <<< custom event handler
var $tags = $(this).closest('.wrapper').find('.tag');
var index = $tags.index(this);
return (index < $tags.length) ? $tags.eq(index + 1) : $();
}).on('click', function(e) {
e.preventDefault();
$(".wrapper .tag").filter(".active").removeClass('active').end().filter(this).addClass('active'); // move the 'active' highlight
// desired click action here
}).filter(".active").trigger('click');
});
Demo
태그를 클릭하면 응답합니다. 이 부부에게 더 많은 사용자 정의 이벤트 처리기를 포함 할 수 있습니다
- 뒤로 및 다음 요소에 대한 '사용'이벤트 처리기, 뒤로 및 다음 요소에 대한
- 는 '사용 안 함'이벤트 핸들러.
Demo
노트
$(document).ready(function() {
$(".nav .back").on('click', function(e) {
e.preventDefault();
if(this.href) { $(".wrapper .active").triggerHandler('findPrev').click(); } // find previous tag and 'click' it.
});
$(".nav .next").on('click', function(e) {
e.preventDefault();
if(this.href) { $(".wrapper .active").triggerHandler('findNext').click(); } // find next tag and 'click' it.
});
$(".nav .back, .nav .next").on('enable', function() { // <<< custom event handler
$(this).attr('href', '#'); // enable
}).on('disable', function() { // <<< custom event handler
$(this).removeAttr('href'); // disable
});
$(".tag").on('findPrev', function() { // <<< custom event handler
var $tags = $(this).closest('.wrapper').find('.tag');
var index = $tags.index(this);
return (index > 0) ? $tags.eq(index - 1) : $();
}).on('findNext', function() { // <<< custom event handler
var $tags = $(this).closest('.wrapper').find('.tag');
var index = $tags.index(this);
return (index < $tags.length) ? $tags.eq(index + 1) : $();
}).on('click', function(e) {
e.preventDefault();
$(".wrapper .tag").filter(".active").removeClass('active').end().filter(this).addClass('active'); // move the 'active' highlight
$(".nav .back").trigger($(this).triggerHandler('findPrev').length ? 'enable' : 'disable'); // manage the back button
$(".nav .next").trigger($(this).triggerHandler('findNext').length ? 'enable' : 'disable'); // manage the next button
// desired click action here
}).filter(".active").trigger('click'); // trigger 'click' to initialize everything
});
: 모두
.trigger()
및
.triggerHandler()
의
- 사용 가능성이 혼란 스럽다. 차이점은 반환되는 것입니다.
.trigger()
은 항상 jQuery (연결 용)를 반환하는 반면 .triggerHandler()
은 핸들러가 반환하는 값을 반환합니다.
- HTML
<button>
요소가 하이퍼 링크 대신 뒤로 및 다음 요소로 약간 단순화됩니다. 올바른 버튼은 본질적으로 href
속성을 망칠 필요없이 비활성화되거나 활성화 될 수 있습니다.
- 커스텀 이벤트는 jQuery 플러그인으로 표현할 수 있는데, 이는 단순한 기능에 대해서는 실행 가능하지만 틀림없이 over-the-top이다.
이것은 또한 나를 위해 일했습니다! 배우기를 시작하면서 어떻게 다른 것들이 가능하고 여전히 똑같은 일을하는지 봅니다. – spicedham