2012-02-01 2 views
0

나는 sizzle 예제를 사용하고 거기에 마지막 코드에서 window.onload가 있습니다.jQuery에서 window.onload

이것은 무엇을 의미합니까?

내 코드는 다음과 같다 :

var init = function() { 
    var trail = Sizzle; 
    var foo = trail('.foo'); 
    foo[0].onmouseover = function() { 
     this.style.backgroundColor = (this.style.backgroundColor.indexOf('blue') === 0) ? 'yellow' : 'blue'; 
    }; 
    foo[1].onmouseover = function() { 
     this.style.backgroundColor = (this.style.backgroundColor.indexOf('red') === 0) ? 'yellow' : 'red'; 
     jQuery(foo[1]).after("<b>Hello</b>"); 
    } 
    foo[2].onmouseover = function() { 
     this.style.backgroundColor = (this.style.backgroundColor.indexOf('green') === 0) ? 'yellow' : 'green'; 
     jQuery(foo[3]).toggle("slow"); 
     jQuery('b').remove(); 
    } 
}; 
window.onload = init; 

이 무엇을 의미합니까?

+2

함수 init()은 창 (페이지)로드 완료 후에 만 ​​작동하기 시작합니다. – Narek

답변

2

이것은 윈도우가로드 될 때 function init이 실행됨을 의미합니다.

6

onload 이벤트를 처리하기 위해 init 함수를 설정한다는 의미입니다. init 함수는 페이지의 모든 내용이로드 될 때 호출됩니다.

jQuery를 사용할 때 대신 jQuery 이벤트를 사용해야합니다. (당신이 체인에 핸들러 코드를 작성하지 않는 한)이 DOM 이벤트는 하나의 핸들러를 가질 수 있지만, jQuery를 이벤트는 여러 핸들러를 가질 수 있습니다 : 당신이 페이지에 하나 이상의 스크립트가있는 경우

$(document).load(init); 

onload 이벤트를 사용하는 , 하나는 다른 것을 대체 할 것입니다. jQuery 이벤트를 사용하더라도 DOM 이벤트를 연결하는 스크립트가 this question과 같이 jQuery 이벤트 핸들러를 인계받습니다. 모든 스크립트에서 jQuery 이벤트를 사용하면이를 해결할 수 있습니다.

+0

왜 downvote? 대답에 대해 잘못 생각한다고 설명하지 않으면 개선 할 수 없습니다. – Guffa

+0

window.onload ≠ document.onload. OP는 window.onload()가 무엇을하는지 알고 싶어하고 document.onload를 사용하도록합니다. 왜? – mkoistinen

+0

미친 것은, 당신은 이미 이것을 알고 있습니다! http://stackoverflow.com/questions/3698200/window-onload-vs-document-ready – mkoistinen

관련 문제