如何在Javascript中编写一个简单的hello world程序?

时间:2017-01-05 10:17:17

标签: javascript jquery html

我一直在寻找世界各地。

我所看到的只是示例警告你好世界

我不想提醒你好世界。

我想打印一个简单的网站,上面写着你好世界。

<!DOCTYPE html>
<html lang="en">
<head></head>
<body id="home">
    <script>
        //print hello world
    </script>
</body>
</html>

javascript是否有打印命令?

以下是网络上的典型示例

http://groups.engin.umd.umich.edu/CIS/course.des/cis400/javascript/hellow.html

4 个答案:

答案 0 :(得分:3)

您可以在身体上添加Text node

&#13;
&#13;
document.body.appendChild(document.createTextNode('Hello World!'));
&#13;
&#13;
&#13;

答案 1 :(得分:2)

在JS代码中,您不会“打印”到屏幕上。而是修改DOM中HTML元素的属性。

要执行您需要的操作,您可以检索#home元素,然后设置其文本。下面的任何一个都适合你:

// POJS
document.getElementById('home').textContent = 'hello world';

// jQuery
$(function() {  
    $('#home').text('hello world'); 
});

<!DOCTYPE html>
<html lang="en">
<head></head>
<body id="home">
    <script>
        document.getElementById('home').textContent = 'hello world';
    </script>
</body>
</html>

答案 2 :(得分:1)

在JavaScript中将一些文本直接写入HTML文档使用document.write();

如下所示。

<script>
document.write("Hello World!");
</script> 

&#13;
&#13;
<!DOCTYPE html>
<html>
<body>

<script>
document.write("Hello World!");
</script>

</body>
</html>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以使用:

document.write("hello world");

但我不会推荐它(有关更多信息,请查看 here)。

你可以改用这个:

document.getElementById("home").textContent = "hello world";
相关问题