2012-04-07 46 views
4

当我调用JavaScript函数B时,Firefox中的JavaScript控制台表示未定义函数A,但是在Chrome浏览器中定义了函数A.当我打电话功能在主体段“A”:为什么我的JavaScript函数“a”没有定义?

<input type="button" onclick="A()" value=" ..A.. "> 

火狐说没有定义的函数B。为什么?

<html> 
    <head> 
     <script language="javascript" type="text/javascript"> 
      function B() { 
       alert(" hi B "); 
       document.write('<br><br><input type="button" onClick="A()" value=" ..A..">'); 
      }; 

      function A() { 
       alert(" hi A"); 
       document.write('<br><br><input type="button" onclick="B()" value=" ..b..">'); 
       if (window.WebCL == undefined) { 
        alert("Unfortunately your system does not support WebCL. "); 
        return false; 
       } 
      } 
     </script> 
    </head> 
    <body> 
     <input type="button" onclick="B()" value=" ..B.. "> 
    </body> 
</html> 
+0

[小提琴](http://jsfiddle.net/F8Dwk/) – 2012-04-07 16:46:20

+0

@TomaszNurkiewicz为小提琴,Chrome和FF给我的警告,但没有给出错误 – Pheonix 2012-04-07 17:23:54

+0

@ Pheonix:你有没有得到“嗨B”和“嗨A”警报?在FF11上,我收到“hi B”警报,但按下按钮A时出现控制台错误。 – 2012-04-07 17:25:39

回答

4

第一次写清除文件的内容,从而在功能上被未定义

+0

这可能是原因。但是这不应该发生,因为该函数在从“

3

的问题是,您所呼叫document.write在页面加载后,有效地消灭了的现有内容页面,包括嵌入式脚本。您应该使用DOM manipulation methods

1

试试这个:

<html> 
<head> 
<script language="javascript" type="text/javascript"> 

var A = function() { 
    alert(" hi A"); 
    document.write('<br><br><input type="button" onclick="B()" value=" ..b..">'); 
    if (window.WebCL == undefined) { 
    alert("Unfortunately your system does not support WebCL. "); 
    return false; 
    } 
} 

function B(){ 
    alert(" hi B ");  
    document.body.innerHTML = ('<br><br><input type="button" onClick="new A()" value=" ..A..">'); 
} 

    </script> 
    </head> 

<body> 
    <input type="button" onclick="B()" value=" ..B.. "> 
</body> 

</html> 
+0

我接受了Ehsan的形式,并且我去研究DOM方法,谢谢 – 4l3x 2012-04-08 04:29:21

相关问题