使用JavaScript隐藏/显示表 [英] Hiding / Showing a Table with JavaScript
问题描述
我有一些数据,我有扩展和折叠按钮那里,如果我们点击+它将展开和显示表,如果我们点击 - 它将崩溃,我使用以下代码,但我得到错误与
hi i have atable with some data and i have expand and Collapse button there if we click on + it will expand and show table and if we click on-it will collapse and i am using following code but i am getting error with
document.getElementById('eleName');
imageXchk='expand';
loadedCheck='false';
function toggleDisplayCheck(e, tableSize){
element = document.getElementById(e).style;
if (element.display=='none') {
element.display='block';
}
else {
element.display='none';
}
if (loadedCheck=='false') {
myUpdater('returnsDetailsTable', '/oasis/faces/merchant/dashboard/ReturnsDetailsCheck.jsp', { method: 'get' });
loadedCheck='true'
}
size = tableSize-1;
eleName = 'mercPerfDashboardForm:returnsDetailsTable:' + size +':switchimageRetChk'
if (imageXchk=='collapse') {
document.getElementById('eleName').src='${pageContext.request.contextPath}/images/expand.gif';imageXchk='expand';
}
else {
document.getElementById('eleName').src='${pageContext.request.contextPath}/images/collapse.gif';imageXchk='collapse';
}
return false;
}
推荐答案
如果元素的样式是 display
属性以前没有在JavaScript中显式设置,它将为空,即使该元素是通过某些CSS规则隐藏的。最简单的方法是知道初始状态(隐藏或可见)是什么,并假设该状态是否显示
属性为空:
If the element's style's display
property hasn't been explicitly set previously in JavaScript, it will be empty, even if the element is hidden via some CSS rule. The easiest thing to do would be to know what the initial state (hidden or visible) is and assume that state if the display
property is empty:
if (element.display=='none' || element.display=='') {
element.display='block';
}
else {
element.display='none';
}
这篇关于使用JavaScript隐藏/显示表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!