为什么我的JavaScript函数是;a「;没有定义?

为什么我的JavaScript函数是;a「;没有定义?,javascript,Javascript,当我调用JavaScript函数B时,Firefox中的JavaScript控制台说函数A没有定义,但在Chrome浏览器上定义了。当我在身体部位调用函数“A”时: <input type="button" onclick="A()" value=" ..A.. "> Firefox说函数B没有定义。为什么? <html> <head> <script language="javascript" type="text/jav

当我调用JavaScript函数B时,Firefox中的JavaScript控制台说函数A没有定义,但在Chrome浏览器上定义了。当我在身体部位调用函数“A”时:

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

Firefox说函数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>

函数B(){
警报(“hi B”);
文件。写(“

”); }; 函数A(){ 警惕(“你好”); 文件。写(“

”); 如果(window.WebCL==未定义){ 警报(“很遗憾,您的系统不支持WebCL。”); 返回false; } }
第一次写入将清除文档内容,导致函数A未定义

问题在于您正在调用
文档。在加载页面后写入
,这将有效地清除页面的现有内容,包括嵌入的脚本。您应该改为使用。

试试这个:

<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>

var A=函数(){
警惕(“你好”);
文件。写(“

”); 如果(window.WebCL==未定义){ 警报(“很遗憾,您的系统不支持WebCL。”); 返回false; } } 函数B(){ 警报(“hi B”); document.body.innerHTML=(“

”); }
@TomaszNurkiewicz对于那把小提琴,chrome和FF都给了我提示,没有人给我提示error@Pheonix:您是否同时收到“hi B”和“hi A”警报?在FF11上,我收到“hi B”警报,但按下按钮A时控制台出错。应该有一些更微妙的方法来处理此类问题。您还可以将事件附加到DOM:或者这可能就是原因。但是这不应该发生,因为函数是在从
节点删除之前定义的,请用fiddle检查它。脚本最初在那里,但在单击B(用FF检查)后不再存在。在删除脚本后,不应取消定义函数A。因为当它被定义时,它的引用被附加到窗口对象。我接受了Ehsan的形式,我去研究DOM方法,谢谢