2010-04-13 1 views
2

실례하시기 바랍니다의 .js 페이지에 jQuery를로드 - 어디동적으로 나는 사람들이 자신의 웹 사이트에서 액세스 할 수있는 간단한 위젯을 만들려면, 내가 여기에 간단 해요 경우

<script language="javascript" src="test2.js"></script> 
<div id="test"></div> 

같은 사본 예를/과거의 일 그들의 웹 페이지에서. 여기서 Jquery와 test2.js의 /에있는 함수를 통해 동적으로 채워진다.

JQuery가 실제로 test2.js의 페이지에 "인쇄"되면 할 수 있지만 JQuery를 동적으로 포함/호출하려고하면 JQuery 함수가 작동하지 않습니다. 자바 스크립트를 통해 JQuery를 호출 한 다음 페이지의 함수로 어떻게 작동합니까?

그리고/또는 동적으로 < div id="test"></div>을 추가하는 쉬운 방법이 있습니까? 물론 body.append 수 있지만 그 페이지의 맨 아래에 추가합니다. 스크립트가 포함 된 위치에 .append 할 수있는 방법이 있습니까

<script language="javascript" src="test2.js"></script> 

실제로 있습니까?

희망합니다.

답변

0

에 첨부 할 jQuery 라이브러리를 기다릴 페이지의 하단에 배치해야 :

스크립트에서 document.write으로 전화하면이 내용은 스크립트 다음에 문서에 삽입됩니다.

<html> 
<head></head> 
<body> 
    <div id="first"> 
     <div id="second"> 
      <script type="text/javascript"> 
       document.write('<h2>test</h2>'); 
      </script> 
      <h2>test</h2> 
     </div> 
    </div> 
</body> 
</html> 
에서

예컨대 :

<html> 
<head></head> 
<body> 
    <div id="first"> 
     <div id="second"> 
      <script type="text/javascript"> 
       document.write('<h2>test</h2>'); 
      </script> 
     </div> 
    </div> 
</body> 
</html> 

결과

5
function loadjscssfile(filename, filetype) { 
     if (filetype == "js") { //if filename is a external JavaScript file 
      var fileref = document.createElement('script') 
      fileref.setAttribute("type", "text/javascript") 
      fileref.setAttribute("src", filename) 
     } 
     else if (filetype == "css") { //if filename is an external CSS file 
      var fileref = document.createElement("link") 
      fileref.setAttribute("rel", "stylesheet") 
      fileref.setAttribute("type", "text/css") 
      fileref.setAttribute("href", filename) 
     } 
     if (typeof fileref != "undefined") 
      document.getElementsByTagName("head")[0].appendChild(fileref) 
    } 

    loadjscssfile("jquery-1.4.2.js", "js") //dynamically load and add this .js file 

당신이 JQuery와 코드 지금 <div id="test"></div> 스크립트의 위치에 동적으로 추가 할 수 있는지 여부를 질문에 대해서는 머리

관련 문제