Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在html中调用javascript函数_Javascript_Html_Function - Fatal编程技术网

在html中调用javascript函数

在html中调用javascript函数,javascript,html,function,Javascript,Html,Function,我刚刚完成了代码学院关于javascript的课程,但它没有教我如何在html中编写javascript。我需要使用函数在html的标记中显示hello world,这可能吗?我的朋友告诉我,我需要jquery来完成这项工作。你必须明确告诉网页在加载时执行你的功能 我想你应该把它写在网页的正文中,而不是返回字符串 这是一种简单的方法: <html> <head> </head> <body> <scr

我刚刚完成了代码学院关于javascript的课程,但它没有教我如何在html中编写javascript。我需要使用函数在html的标记中显示hello world,这可能吗?我的朋友告诉我,我需要jquery来完成这项工作。

你必须明确告诉网页在加载时执行你的功能

我想你应该把它写在网页的正文中,而不是返回字符串

这是一种简单的方法:

<html>
    <head>
    </head>

    <body>
        <script>    
            var myFunction = function(){
                return "hello world";
            }
        </script>    
        myFunction();        
    </body>
</html>
试试这个

<html>
    <head>
        <script>
            var myFunction = function(){
                document.body.innerHTML = "hello world";
            }
        </script>
    </head>
    <body onload='myFunction();'>
    </body>
</html>
这已经被问到了


我只是用document替换标记。write

将其包装在taguse document中。write to write in HTML代替return。脚本标记必须包含在head标记中。这根本不是真的。但是,这是为数不多的在没有糟糕文档的情况下将返回值添加到HTML的答案之一。写。@Cerbrus我已经删除了head-tag语句中的脚本。非常好:-现在这是一个不错的答案。不要使用document.write。这是不可预测的。
<html>
<head>
</head>
<body onload='myFunction()'>

<script type="text/javascript">
    var myFunction = function(){
       return "hello world";
    }
</script>

</body>
</html>
<html>
<head>
</head>
<body>
<Script>

function myFunction(){
    document.write("hello");
}


</script>
<Script>
    myFunction();
    </Script>

</body>
</html>