每次点击JavaScript中的链接时,如何更改文本?

时间:2014-07-20 13:13:10

标签: javascript html5

我想创建一个文本生成器,但是当我点击链接时,它只能生成一些存储在数组中的示例文本。我试过使用多个字符串,但我无法让它工作。这是代码:

HTML:

            <!DOCTYPE html>
            <html>
            <head>
            <meta charset="UTF-8"/>
            <title>Text generate 2 - copy</title>
            <link rel="stylesheet" href="css/style.css">
            </head>
            <body>
            <a href="#" id="link">Generate</a>

            <h1 id="head"></h1>
            <p id="para"></p>


            <script src="js/script.js"></script>

            </body>
            </html>

JS:

            var heading = document.getElementById('head');
            var paragraph = document.getElementById('para');

            var head = ["Sample heading one"];
            var para = ["Sample paragraph one"];

            document.getElementById("link").onclick = function visible() {

            if (heading.style.display = "none") 
            {
            heading.style.display = "block";
            }

            else 
            {
            heading.style.display = "block";
            }

            if (paragraph.style.display = "none") 
            {
            paragraph.style.display = "block";
            }

            else 
            {
            paragraph.style.display = "block";
            }

            heading.innerHTML = head;
            paragraph.innerHTML = para;
            }

JS Bin

0 个答案:

没有答案