Achieve the effect
showTitle: When the display title button is clicked, all The title is displayed
hideTitle: When the hide title button is clicked, all titles are hidden
addCity: When the add city button is clicked , a new city name
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"> <title></title> <script> // function showTitle() { // var titleElement = document.getElementById("title"); // titleElement.style.display = "block"; // } // // function hideTitle() { // var titleElement = document.getElementById("title"); // titleElement.style.display = "none"; // } function addCity() { var newElement = document.createElement("li"); // <li></li> var textNode = document.createTextNode("重庆"); // 重庆 newElement.appendChild(textNode); // <li>重庆</li> document.getElementById("city").appendChild(newElement); } </script></head><body><h3 id="title" style="display: block">八维学院</h3><h3 id="title222" style="display: block">八维学院222</h3><p>选择城市</p><ul id="city"> <li>北京</li> <li id="city_2">上海</li> <li>天津</li></ul><input type="button" onclick="showTitle()" value="显示标题"/><input type="button" onclick="hideTitle()" value="隐藏标题"/><input type="button" onclick="addCity()" value="添加城市"/><script> /*window.document.getElementById("title");*/ var arr = document.getElementsByTagName("h3"); console.log(arr[0]); console.log(arr[1]);</script></body></html>
The above is the detailed content of How to control the title and add information through Js code. For more information, please follow other related articles on the PHP Chinese website!