1

我有一个js文件和一个html文件野兔是代码

testjs.js

function DynamicDiv() {  
    alert('enter');            
        var dynDiv = document.createElement("div");
        dynDiv.id = "divDyna";
        dynDiv.innerHTML = "Created using JavaScript";
        dynDiv.style.height = "20px";
        dynDiv.style.width = "300px"; 
        dynDiv.style.backgroundColor = 'gray';
        document.body.appendChild(dynDiv);
    }

我的html代码如下

<html>
<head>
<TITLE>Test Page</TITLE>
<script type="text/javascript" src="testjs.js" language="javascript"></script>
<script type="text/javascript" src="jquery-1.4.2.js" language="javascript"></script>
<script type="text/javascript" language="javascript">
    //"Global" variable accessible to all 
    //$(document).ready(DynamicDiv());  
</script>
</head>
<body onLoad="DynamicDiv()"> 
 <input id="Button1" type="button" value="Using JS" onclick="" />   
<div id="Layer1" style="position: absolute; width: 100%; height: 17px; z-index: 1; left: 0px; top: 3px;">
  <div align="right"><a href="http://www.uspto.gov/main/patents.htm">PATENTS</a>&nbsp;&nbsp;&nbsp; </div>
</div>
</body>
</html>

但我无法调用该函数

4

4 回答 4

1

well your code is woking well with me, may be the path that you have provided to your testjs.js file might not be accurate but its workin fine with me. try checking the path to your js file

于 2012-05-18T09:04:55.553 回答
1

另一个建议,因为你加载 jQuery,加载最新的并更改脚本以使用它

<html>
  <head>
    <TITLE>Test Page</TITLE>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js" language="javascript"></script>
    <script type="text/javascript" language="javascript">
    function DynamicDiv() {  // to be externalised
    //    alert('enter');            
      $('<div/>', {
        id: 'divDyna',
        width:300,
        height:20,
        css:{border:'1px solid black', 'background-color':'grey'},
        html: 'Created using <i>JavaScript</i>'
      }).appendTo('body')
    } 
    </script>
    <script type="text/javascript" language="javascript">     
      $(document).ready(function() {
        DynamicDiv();  
      });  
    </script>
  </head>
  <body> 
    <input id="Button1" type="button" value="Using JS" onclick="" />   
    <div id="Layer1" style="position: absolute; width: 100%; height: 17px; z-index: 1; left: 0px; top: 3px;">
      <div align="right"><a href="http://www.uspto.gov/main/patents.htm">PATENTS</a>&nbsp;&nbsp;&nbsp;         
      </div>
    </div>
  </body>
</html>
于 2012-05-18T09:22:26.093 回答
0

我使用外部 js 文件进行了测试。即使在 IE6 上也可以正常工作。此类问题多为“文件路径错误”。

于 2012-05-18T09:48:55.430 回答
0

您的代码与我配合良好,可能您的 Internet 浏览器不兼容,或者您​​提供给testjs.js文件的路径可能不正确。检查 JavaScript 文件的路径。

于 2013-04-12T03:46:22.347 回答