为什么我无法console.log()style.display

时间:2019-12-24 09:37:59

标签: javascript html css console.log

我在尝试console.log()菜单内容的显示值时遇到问题,但是即使我选择正确也不能,请帮我检查一下,谢谢

clickMenuReponsive.onclick = function(){
    let menuContent = document.querySelector('nav .menu .menu-content');
    console.log(menuContent);
    console.log(menuContent.style.display);
}
nav .menu .menu-content {display: none; margin-top: 10px}

enter image description here

1 个答案:

答案 0 :(得分:0)

要获取元素的 非内联 样式,可以使用window.getComputedStyle()

演示:

function fixedDelimeter(str){

return str.split('-').reverse().join('-')

}

console.log('With Fixed Delimeter: ',fixedDelimeter('subject-verb-object'))


function dynamicDelimeter(str,delimeter){

return str.split(delimeter).reverse().join(delimeter)

}

console.log('With Dynamic Delimeter: ',dynamicDelimeter('subject*verb*object', '*'))
let menuContent = document.querySelector('.menu-content');
console.log(menuContent);
console.log(window.getComputedStyle(menuContent).display);
.menu-content {display: none; margin-top: 10px}