我正在研究一些javascript示例,我只是做了这个:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Page title</title>
<script type="text/javascript">
function displayText()
{
document.getElementById('targetDIV').innerHTML = "You're using Javascript";
}
</script>
</head>
<body onload="displayText()">
<h2>This should be before the other text.</h2>
<div id="targetDIV">
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
好.非常基本的,我知道 - 但我意识到我对某些事情的"原因"感到困惑.可以准确地说:
功能= 会发生什么.
调用(正文载入......)= 什么时候会发生.
和DIV ID = "targetDIV"= WHERE它会发生
我知道这个例子就是这种情况,但一般情况下Javascript的工作方式是什么?
Yes, that's a pretty good working model to carry in your head.
onload for the body is called an Event and many objects issue events. Your function displayText is called in response to the onload Event and is therefore an event handler.
The code inside your function can do anything, but in this case it dynamically loads some text into a tag on your page.
There are a couple of other things worth pointing out at this point. You access the tag using document.getElementById. document is variable available to you in Javascript which contains a model of the page called the DOM or document object model. This is extremely powerful as it presents a hierarchical layout of everything on your page and allows you to manipulate the contents.
getElementById() is a very useful function which searches the DOM tree and returns the object which has the ID that you specify, it's a sort of search. The text gets to your tag because you added the targetDIV id to the DIV tag and therefore you could find it via the DOM function.
欢迎使用Javascript编程.现在你有一个好的工作模型,你会发现你可以做的很多非常聪明的事情,你作为一个网络程序员的生活将永远不会再次相同.
| 归档时间: |
|
| 查看次数: |
81 次 |
| 最近记录: |