简体   繁体   English

使用JavaScript更改内联CSS

[英]Changing inline CSS with JavaScript

I have been trying to change this div tag's style when an image is clicked, and its not working. 我一直在尝试单击图像时更改此div标签的样式,并且该样式不起作用。 First I want to check and see if the style is "display:none;" 首先,我想检查一下样式是否为“ display:none;”。 or "display:block". 或“ display:block”。 But when I try to alert the value it returns "undefined" for both when the display is set to none and when it is set to block. 但是,当我尝试警告该值时,无论是在显示设置为无时还是在设置为阻止时,它都返回“ undefined”。

<div id="navbar-mobile-id" style="display:block;" class="navbar-mobile">

This is in my function: (function works when clicked but alerts "undefined") 这在我的函数中:(函数在单击时起作用,但会提示“未定义”)

alert(console.log(document.getElementById("navbar-mobile-id").style.display));

This should alert either "block" or "none" 这应该提醒“阻止”或“无”

The return value of console.log() is undefined console.log()的返回值undefined

If you want to alert the value of the display property, then you have to alert that . 如果你想提醒值display属性,那么你必须提醒

alert(document.getElementById("navbar-mobile-id").style.display);

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

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