赞
踩
JavaScript 可以通过以下方式在页面中输出内容:
使用 document.write() 方法,将文本字符串直接写入 HTML 文档中。
document.write("Hello World!");
使用 innerHTML 属性,向元素的内部插入 HTML 代码。
document.getElementById("myElement").innerHTML = "Hello World!";
使用 console.log() 方法,在浏览器的控制台中输出内容。
console.log("Hello World!");
在页面中创建新的 HTML 元素,并将其添加到文档中
- var newElement = document.createElement("p");
- newElement.textContent = "Hello World!";
- document.body.appendChild(newElement);
将内容作为提示框或对话框的文本输出
- alert("Hello World!");
- prompt("What is your name?");
- confirm("Are you sure?");
值得注意的是,使用 document.write() 方法会覆盖页面原有的内容,不建议在实际开发中使用。而其他方法则可以更灵活地处理输出内容,建议根据具体需求选择使用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。