Home  >  Article  >  Web Front-end  >  How to control the title and add information through Js code

How to control the title and add information through Js code

一个新手
一个新手Original
2017-10-10 10:02:111561browse


  • 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>
will be created under the original city list.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn