2 回答
TA贡献1812条经验 获得超5个赞
您应该放在[0]selector 之后document.getElementsByClassName("panel"),因为document.getElementsByClassName("panel")返回一个带有类“panel”的元素列表(数组),那么我建议使用 ID 并document.getElementById("panel_id");改为 put 以避免这些问题。
这是您的代码:(带类)
window.onload = function (){
var wrapperHeight = document.getElementsByClassName("panel")[0].outerHeight;
var innerHeight = document.getElementsByClassName("description-content")[0].outerHeight;
var button = document.getElementsByClassName("accordion")[0];
if(innerHeight > wrapperHeight){
button.style.display = "block";
}
alert(innerHeight);
}
这是您的代码(带有 ID):
window.onload = function (){
var wrapperHeight = document.getElementById("panel_id").outerHeight;
var innerHeight = document.getElementById("description-content_id").outerHeight;
var button = document.getElementById("accordion_id");
if(innerHeight > wrapperHeight){
button.style.display = "block";
}
alert(innerHeight);
}
TA贡献1796条经验 获得超4个赞
您需要使用 style.height 属性。试试这个:
window.onload = function (){
var wrapperHeight = document.getElementsByClassName("panel")[0].style.height;
var innerHeight = document.getElementsByClassName("description-content")[0].style.height;
var button = document.getElementsByClassName("accordion");
if(innerHeight > wrapperHeight){
button.style.display = "block";
}
alert(innerHeight);
}
添加回答
举报