当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS 怎样使用
The HTML <script> tag is used to insert a JavaScript into an HTML page.
HTML中的 <script> 标签用于在HTML页面中插入JavaScript。
Write text
写入文字
How to write text on a page.
如何向一个页面上写入文字。
Write text with formatting
如何写入带格式的文字
How to format the text on your page with HTML tags.
如何用HTML标签格式化你所要写入的文字。
<html> |
The code above will produce this output on an HTML page:
上述代码将在HTML页面上输出下面的文字:
Hello World! |
To insert a JavaScript into an HTML page, we use the <script> tag (also use the type attribute to define the scripting language).
我们通过 <script> 标签在HMTL页面中插入JavaScript(同时我们也可以使用 type 属性来定义所要插入的脚本语言)。
So, the <script type="text/javascript"> and </script> tells where the JavaScript starts and ends:
<script type="text/javascript">和</script>分别标记了JavaScript代码的开始和结束:
<html> |
The word document.write is a standard JavaScript command for writing output to a page.
document.write 是用于页面输出的标准JavaScript命令。
By entering the document.write command between the <script type="text/javascript"> and </script> tags, the browser will recognize it as a JavaScript command and execute the code line. In this case the browser will write Hello World! to the page:
将document.write语句写入到<script type="text/javascript">和</script>标签之间,浏览器就可以将它识别为JavaScript命令并执行它。下例的执行结果为浏览器向页面输出 Hello World!
<html> |
Note: If we had not entered the <script> tag, the browser would have treated the document.write("Hello World!") command as pure text, and just write the entire line on the page.
注意:如果我们没有使用<script>标签,浏览器将把 document.write("Hello Word!")语句识别为普通文本,并将其全部输出。
With traditional programming languages, like C++ and Java, each code statement has to end with a semicolon.
与C++和Java语言一样,JavaScript也是使用分号来结束一条语句。
Many programmers continue this habit when writing JavaScript, but in general, semicolons are optional! However, semicolons are required if you want to put more than one statement on a single line.
许多的程序员在写JavaScript时都有使用分号来结束语句的习惯,但一般情况下分号只当你需要在同一行写入多条语句时才必须使用。
Browsers that do not support JavaScript will display the script as page content. To prevent them from doing this, we may use the HTML comment tag:
不支持JavaScript的浏览器将把其当作页面文本输出。为避免这种情况,我们可以使用HTML注释标签:
<script type="text/javascript"> |
The two forward slashes at the end of comment line (//) are a JavaScript comment symbol. This prevents the JavaScript compiler from compiling the line.
在注释标签结束符前的两斜杆(//)是JavaScript命令的标记。它表示JavaScript语句的结束。