당신은 데릭의 답변을 시도 할 수 있지만 곧 querySelector가 쿼크 모드 또는 IE 7 이하에서 IE8에서 지원하지 않는 것을 발견하고 그 addEventListener는 IE 8 이하에서 전혀 지원되지 않습니다. 일반 JS에 대한
의 선택은 자신의 getElementsByClassname 기능 (특히 어려운되지 않음)를 작성하거나 각 이벤트에 대해 하나 개의 수신기를 연결하고 간단한 hasClass 기능을 필요 있도록 이벤트 위임을 사용하는 것을 포함한다.
어쨌든
, 여기에 예입니다 :
어떤 javscripter 도서관에있는 또는 몇 분 안에 코딩 할 수 있어야합니다 일부 표준 라이브러리 함수 :
// Minimalist addEvent function, ok for the current web
function addEvent(el, fn, evt) {
if (el.addEventListener) {
el.addEventListener(evt, fn, false)
} else if (el.attachEvent) {
el.attachEvent('on' + evt, fn);
}
}
// Returns true or false depending on whether element el has class classname
function hasClassname(el, classname) {
var re = new RegExp('(^|\\s)' + classname + '(\\s|$)');
return re.test(el.className);
}
작업을 수행하는 기능입니다 :
// Toggle the id. Note that this deals with the case where
// the element to toggle isn't found to prevent errors being thrown
function toggleId(e) {
e = e || window.event;
var el = e.target || e.srcElement;
var o;
if (hasClassname(el, 'tomouseover')) {
if (e.type == 'mouseover') {
o = document.getElementById('set1');
if (o) o.id = 'set2';
} else if (e.type == 'mouseout') {
o = document.getElementById('set2');
if (o) o.id = 'set1';
}
}
}
// Attach the listener onload, could also add from a bottom
// script or inline. If inline, use `toggleId(event)`
window.onload = function() {
addEvent(document.body, toggleId, 'mouseover');
addEvent(document.body, toggleId, 'mouseout');
}
위 다시 그렇게 IE6 및 NN 3에 모든 브라우저에서 작동하며 W3C 또는 IE 이벤트 모델 중 하나입니다 미래의 브라우저에서 작동 할 것입니다.
jquery를 사용할 수 있습니까? –
나는 선호하지 않는다. 이것은 Google 가제트에 있습니다. 그래도 할 수 있었다. – Wagtail