2014-03-29 2 views
0

Google 크롬 확장 프로그램을 만들려고하는데 onclick 메소드가 작동하지 않는 것 같습니다.크롬 확장 프로그램에서 onclick이 작동하지 않습니까?

<!DOCTYPE html> 
<html> 
<body> 
<p id="demo">Before clicking button</p> 
<button type="button" id="thing">Pls work</button> 
<script src="popup.js"></script> 
</body> 
</html> 

그리고 자바 스크립트 파일 popup.js :

var a =0; 
function myFunction() 
{ 
    a= a +1; 
    document.getElementById('demo').textContent=a; 
} 
document.getElementById('thing').onclick = myFunction(); 

그리고 마지막으로, 매니페스트 파일 : 나는 HTML 파일 popup.html이

{ 
    "name": "Hello World!", 
    "version": "1.0", 
    "manifest_version": 2, 
    "description": "My first Chrome extension.", 
    "browser_action": { 
    "default_icon": "shark.png", 
    "default_popup": "popup.html" 
    } 
} 

내가이 확장을로드 Chrome에 추가 한 다음 확장 프로그램을 클릭하면 "버튼을 클릭하기 전에"라는 메시지가 아닌 "1"이라는 메시지가 바로 표시됩니다. 그런 다음 단추를 클릭하려고해도 아무 것도 일어나지 않습니다. 즉, 텍스트는 1로 유지되고 늘려야하는 것처럼 증가하지 않습니다.

정말 고마워요 !!

답변

2

이 시도 :

JS :

window.onload = function() { 
    var a =0; 
    function myFunction() 
    { 
     a= a +1; 
     document.getElementById('demo').textContent=a; 
    } 
    document.getElementById('thing').onclick = myFunction; 
} 

HTML이

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript" src="popup.js"></script> 
</head> 
<body> 
<p id="demo">Before clicking button</p> 
<button type="button" id="thing">Pls work</button> 
</body> 
</html> 
+0

정말 고마워요 !!! 그게 내가 필요한 것! – dizzyshark

+0

귀하의 필요에 부응한다면, 귀하는 답변에 투표를하고 답변을 질문으로 설정할 수 있습니다. – csanonymus

0

변경해야하는 유일한 생각은

document.getElementById('thing').onclick = myFunction(); 

입니다

document.getElementById('thing').onclick = myFunction; 

첫 번째 on은 onclick 이벤트에 대해 실행할 함수를 설정하는 대신 함수를 실행합니다.

관련 문제