最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to write a simple hello world program in Javascript? - Stack Overflow

programmeradmin0浏览0评论

I've been looking all over the world.

All I see is samples alerting hello world

I don't want to alert hello world.

I want to print a simple website saying hello world.

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

Does javascript have a print mand?

Here are typical samples on the web

.des/cis400/javascript/hellow.html

I've been looking all over the world.

All I see is samples alerting hello world

I don't want to alert hello world.

I want to print a simple website saying hello world.

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

Does javascript have a print mand?

Here are typical samples on the web

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

Share Improve this question edited Jan 5, 2017 at 10:24 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Jan 5, 2017 at 10:17 user4951user4951 33.2k55 gold badges182 silver badges289 bronze badges 5
  • @RoryMcCrossan I thought that as well, but it doesn't make the website print hello world ;). – roberrrt-s Commented Jan 5, 2017 at 10:19
  • 1 JS: document.getElementById('home').textContent = 'hello world'; jQ: $(function() { $('#home').text('hello world'); }); – Rory McCrossan Commented Jan 5, 2017 at 10:19
  • console.log is that you want to get accustomed to for testing purposes. There is no 'printing' on the page as the page itself consists of elements which you need to refer to. – Dellirium Commented Jan 5, 2017 at 10:20
  • javascript.info/tutorial/hello-world – Nagaraju Commented Jan 5, 2017 at 10:22
  • You'll need to define what you mean by "print" - for most people "print" means, "send to a printer", which doesn't seem likely. Every language uses a different term for what(whatever) you are trying to do. – fdomn-m Commented Jan 5, 2017 at 10:39
Add a ment  | 

5 Answers 5

Reset to default 4

You could append a Text node to the body.

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

In JS code you don't 'print' to the screen. Instead you amend the properties of the HTML elements in the DOM.

To do what you require you can retrieve the #home element then set its text. Either of the below will work for you:

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

In JavaScript Write some text directly to the HTML document Use document.write();.

like below.

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

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

You can use:

document.write("hello world");

but I will not remend it (for more information look here).

You can use this instead:

document.getElementById("home").textContent = "hello world";
console.log("Hello World!")
发布评论

评论列表(0)

  1. 暂无评论