简体   繁体   English

JavaScript函数定义

[英]Javascript function define

I'm currently new to javascript programming. 我目前是javascript编程的新手。 And I have a question about function. 我对功能有疑问。 When I tried to modify html DOM. 当我尝试修改html DOM时。 I could either use function or no function. 我可以使用功能,也可以不使用功能。

The function one is as below: 功能一如下:

<button type="button" onclick="myFunction()">Click Me!</button>

<p id="demo">This is a demonstration.</p>

<script>
function myFunction() { 
document.getElementById("demo").innerHTML = "Hello";
}

And the no function one is here: 这里没有功能:

<p id="demo">This is a demonstration.</p>
<button type="button" onclick= "getElementById('demo').innerHTML= 'Hello'">Click Me!             </button>

My question is :Why there it is document.getElementById()... in function case , but getElementById() in non function case? 我的问题是:为什么在函数情况下为document.getElementById()...在非函数情况下为getElementById()? What's the document. 什么文件。 for? 对于?

Thanks!! 谢谢!!

What's the document. 什么文件。 for? 对于?

To access the document object , where the getElementById method resides. 要访问document对象getElementById方法所在。

Why there it is document.getElementById()... in function case , but getElementById() in non function case? 为什么在函数情况下有document.getElementById()...在非函数情况下有getElementById()?

It should always be there. 它应该一直在那里。 In the inline event handler case, it can be omitted however (which is considered bad practise, though). 在内联事件处理程序的情况下, 可以将其省略 (不过,这被认为是不好的做法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM