请在预置代码中编写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>
2 回答
已采纳
莲_蓶濏__
TA贡献25条经验 获得超3个赞
<!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>
莲_蓶濏__
TA贡献25条经验 获得超3个赞
<!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>
添加回答
举报
0/150
提交
取消