带一个参数的函数

请在预置代码中编写Javascript代码,使预览页面实现如下效果:

点击Morning按钮,引用id为demo的段落内容显示Good Morning!;
点击Evening按钮,引用id为demo的段落内容显示Good Evening!;

任务与要求

请定义函数myFunction,传入一个参数text,使id为demo的段落返回text的值

请在form标签内创建一个按钮,值为Morning,调用函数myFunction,点击按钮,段落内容显示Good Morning!

请在form标签内创建一个按钮,值为Evening,调用函数myFunction,点击按钮,段落内容显示Good Evening!

<!DOCTYPE html>

<html> 

<head> 

</head> 

<body>

<form> 

</form> 

<p id="demo"></p>

</body> 

</html>


qq_慕标6304929
浏览 1012回答 2
2回答

莲_蓶濏__

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <form> <button onclick="myFunction('Morning')"  type="button">Morning</button> <button onclick="myFunction('Evening')"  type="button">Evening</button> </form> <p id="demo"></p> <script> function myFunction(text) { document.getElementById('demo').innerHTML = 'Good ' + text + '!'; } </script> </body> </html>

莲_蓶濏__

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <form></form> <div id="demo"></div> <script> function myFunction(text) { document.getElementById('demo').textContent = 'Good ' + text + '!;'; } var oBtnMor = document.createElement('input'); oBtnMor.type = "button"; oBtnMor.onclick = function() { myFunction('Morning'); }; oBtnMor.value = 'Morning'; document.forms[0].appendChild(oBtnMor); var oBtnEve = document.createElement('input'); oBtnEve.type = "button"; oBtnEve.onclick = function() { myFunction('Evening'); }; oBtnEve.value = 'Evening'; document.forms[0].appendChild(oBtnEve); </script> </body> </html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript