简体   繁体   English

尝试通过单击事件侦听器使用 Javascript 更改显示样式

[英]Trying to change the display style using Javascript through a click event listener

This code of mine is not working.我的这段代码不起作用。

document.getElementById("information-read-more").addEventListener("click", function(){
    document.getElementsByClassName("read-more-less-inner").style.display= "block";
});

Trying to build a read more section of my HTML that hides on mobile and reappears when the user clicks a button.尝试构建我的 HTML 的阅读更多部分,该部分隐藏在移动设备上并在用户单击按钮时重新出现。

The style sheet has a media query that sets the display to none on a div with the read-more-less-inner class when the window gets small enough.样式表有一个媒体查询,当窗口变得足够小时,它在具有 read-more-less-inner 类的 div 上将显示设置为 none。 Im wanting this read more button to override the style I made and show it.我想要这个阅读更多按钮来覆盖我制作的样式并显示它。

The event listener fires when I click it, just the style doesnt change.当我单击它时,事件侦听器会触发,只是样式不会改变。

I dont think it makes a difference but im doing the project in a wordpress page.我不认为这有什么不同,但我在 wordpress 页面上做这个项目。

Amys comment was the solution. Amys 的评论是解决方案。 Thanks!谢谢!

Changed it from a class to an ID and it works now.将其从类更改为 ID,现在可以使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM