이 문제와 관련된 다른 모든 질문/해결책을 살펴본 결과 해결책을 찾을 수 없습니다.document.getElementById()는 외부 JS 파일에서 호출 할 때 NULL을 반환합니다.
버튼이있는 기본 aspx 페이지가 있습니다. OnClick은 JS 함수를 호출합니다. Javascript 함수는 작동하는 document.getElementById()를 호출합니다. 그런 다음 외부 JA 파일에있는 하위 함수를 호출하고 동일한 호출이 실패합니다. 왜?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jstest.aspx.cs" Inherits="jstest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox runat="server" ID="RunAtStartup" OnClick="function1();" text="Click Me" />
</div>
</form>
<script>
function function1()
{
if (document.getElementById("<%= RunAtStartup.ClientID %>") == null)
alert('function1 null');
else
alert('function1 not null');
function2();
}
</script>
<script src="./function2.js"></script>
</body>
</html>
그리고 자바 스크립트 파일 function2.js 외부는 기능 1을 보여줍니다 버튼을 클릭의 결과는 'null이 아닌'이며, 기능 2가 '널 (null)'이다
function function2() {
if (document.getElementById("<%= RunAtStartup.ClientID %>") == null)
alert('function2 null');
else
alert('function2 not null');
}
입니다.
문서를 매개 변수로 전달하려고 시도했지만 작동하지 않았습니다. 나는 function2(). bind (document)를 시도했다. 그것은 작동하지 않았다. 나는 자바 스크립트 디버거를 밟았으며 function1의 document 객체가 function2의 document 객체와 동일하게 보입니다. 사전 마이클
은 js 파일의 이름입니다. function2.js ?? –
아래 답변을 참조하십시오. 기본적으로'.js' 파일에 서버 측 코드'<%= ...%>'을 혼합하고 있습니다. – EdSF