페이지의 특정 것이 변경되면 버튼에 onclick 이벤트를 제공하려고합니다. 나는 여러 가지 방법으로 노력했지만 아무도 노력하지 못했습니다. 내가 도대체 뭘 잘못하고있는 겁니까?JavaScript 동적 onClick 문제
다음은 내가 시도한 것입니다.
document.getElementById(subDiv).onclick = function() { alert('Error. There is an error on the the page. Please correct that then submit the page'); };
document.getElementById(subDiv).onclick = "alert('Error. There is an error on the the page. Please correct that then submit the page');";
function redErrorAlert()
{
alert('Error. There is an error on the the page. Please correct that then submit the page');
}
document.getElementById(subDiv).onclick = redErrorAlert;
document.getElementById(subDiv).setAttribute('onclick',redErrorAlert(), false);
document.getElementById(subDiv).setAttribute('onclick','redErrorAlert()', false);
참고 : subDiv는 요소의 ID를 포함하는 변수입니다.
jquery와 같은 js 라이브러리를 사용하는 것이 좋습니다. 오히려 쉽고 크로스 브라우저 호환. 그게 .. .. 어떤 브라우저에서 이것을 테스트하려고합니까? – spinon
@spinon Chrome에서 테스트 중이지만 IE, FF 및 Chrome에서 작동해야합니다. – Jason