当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS 怎样使用

JavaScript
JS 介绍
JS 怎样使用
JS 在哪使用
JS 变量
JS If...Else
JS Switch
JS 操作符
JS Popup Boxes
JS 函数
JS For 循环
JS While 循环
JS Break 循环
JS For...In
JS 事件
JS Try...Catch
JS Throw
JS onerror
JS 特殊字符
JS Guidelines
JS 对象介绍

JavaScript 中的 JS 怎样使用


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1510 ::
收藏到网摘: n/a

The HTML <script> tag is used to insert a JavaScript into an HTML page.
HTML中的 <script> 标签用于在HTML页面中插入JavaScript。


Examples
实例

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标签格式化你所要写入的文字。


How to Put a JavaScript Into an HTML Page
如何在HTML页面中插入JavaScript

<html>
<body>
<script type="text/javascript">
document.write("Hello World!")

</script>
</body>
</html>

The code above will produce this output on an HTML page:
上述代码将在HTML页面上输出下面的文字:

Hello World!

Example Explained
实例分析

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>
<body>
<script type="text/javascript">

...
</script>
</body>
</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>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>

</body>
</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!")语句识别为普通文本,并将其全部输出。


Ending Statements With a Semicolon?
用分号来终止语句?

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时都有使用分号来结束语句的习惯,但一般情况下分号只当你需要在同一行写入多条语句时才必须使用。


How to Handle Older Browsers
如何操作较老版本的浏览器

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

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

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语句的结束。

评论 (4) 1 All

登陆 | 还没注册?