这是我为脚本准备的。在下面,它似乎工作正常,但在我的网站上不起作用。我看过其他建议使用document.querySelector的示例,但我对Js并不十分熟悉,并且在实现示例脚本时遇到了麻烦。谁能说明我做错了什么以及如何更好地编写和压缩脚本?在此先感谢您的时间和帮助!



document.getElementById("btn").onclick=function () {
        document.getElementById("secdes").innerHTML="change";
   }

    document.getElementById("btn1").onclick=function () {
        document.getElementById("secdes").innerHTML="changes";
   }

 <div class="col-md-4 buttons btnbox">
                        <button type="button" name="btn" id="btn" class="btn btn-outline-primary homebtn">Home Page</button>
                        <button type="button" name="btn1" id="btn1" class="btn btn-outline-secondary secondarybtn">Secondary Page</button>
                        <button type="button" name="btn2" id="btn2" class="btn btn-outline-secondary blogbtn">Blog</button>
                        <button type="button" name="btn3" id="btn3" class="btn btn-outline-secondary blogbtn">About Page</button>


                        <p class="rubydes">To create the fantasy world for the viewers each illustration incorporated the theme of "The Yellow Brick Road". </p>

                        <p class="secdes" id="secdes">The challenge of the home page was to seamlessly incorporate the shoe box and "Ruby Slipper" photograph into the illustration.</p>




                    </div>

最佳答案

您可以尝试的方法是为每个按钮赋予唯一的ID,然后从javascript访问它们。

关于javascript - 我有四个按钮,并希望在单击每个按钮时返回唯一的文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41212681/

10-12 06:47