문제

마우스 오버 콜백이있는 IMG 태그를 생성하는 JavaScript 코드가 있으며 IMG 태그를 페이지에 추가합니다. 문제는 콜백이 호출 될 때마다 JavaScript 구문 오류가 (Firefox 콘솔에서) 발생한다는 것입니다.

이 코드는 문제를 보여줍니다 ...

    
        var imgUrl = 'http://sstatic.net/so/img/logo.png';
        var img = document.createElement('img');
        img.setAttribute('src', imgUrl);
        img.setAttribute('onmouseover', function() {
            alert('mouseover ' + imgUrl);
        });
        document.body.appendChild(img);
    

콜백 함수가 빈 함수 일 때 구문 오류가 발생합니다.

누구든지 구문 오류의 원인과 수정 방법을 설명 할 수 있습니까?

(WIN XP에서 FF 3.5.2를 사용하고 있습니다.)

도움이 되었습니까?

해결책

문자열이 예상되는 기능을 전달합니다. 대신 시도해보십시오.

    var imgUrl = 'http://sstatic.net/so/img/logo.png';
    var img = document.createElement('img');
    img.src = imgUrl;
    img.onmouseover = function() {
        alert('mouseover ' + imgUrl);
    };
    document.body.appendChild(img);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top