JavaScript in 10 Steps or Less

來源:互聯網
上載者:User

沒有按別人的推薦,學什麼聖經類的js書,而是隨便挑了本《JavaScript in 10 Steps or Less》。

花了3個小時,看了30個task。

講的非常淺顯詳細。雖然是E文,但很淺顯易懂。

 

task31:

Calling Functions from Tags

One of the benefits of JavaScript is to be able to tie interactivity to elements of the HTML page. One way you can do this is to set up links in HTML that actually trigger calls to JavaScript functions when the link is clicked.

There are two ways to do this:
1. Use the onClick attribute of the a tag to call the function:
<a href=”#” onClick=”functionName()”>Link text</a>
2. Use a javascript: URL in the href attribute of the a tag to call
the function:
<a href=”javascript:functionName()”>Link text</a>

The following task illustrates these two methods of calling a function from a link by creating a function that displays an alert dialog box to the user and then providing two separate links for the user to use to call the function:
1. Open a new HTML document in your preferred HTML or text editor.
2. Create the header of the document with opening and closing head tags:
<head>
</head>
3. Insert a script block in the header of the document:
<script language=”JavaScript”>
<!--
// -->
</script>
4. Create a function named hello that takes no arguments:
function hello() {
}
5. In the function, use the window.alert method to display an alert
dialog box:
window.alert(“Hello”);
6. Create the body of the document with opening and closing body tags.
7. In the final page create two links that call the hello function using
onClick and the javascript: URL techniques so that the final
page looks like Listing 31-1.
<head>
<script language=”JavaScript”>
<!--
function hello() {
window.alert(“Hello”);
}
// -->
</script>
</head>
<body>
<a href=”#” onClick=”hello();”>Call hello() from
onClick.</a>
<br>
<a href=”javascript:hello();”>Cal hello() from href.</a>
</body>
Listing 31-1: Calling a function from a link.
8. Save the file.
9. Open the file in a browser, and you should see two links in the
browser.
10. Click on either link and you should see a dialog box.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.