在给定行索引和表 ID 的情况下隐藏/取消隐藏表行
Hide/unhide table row given the row index and table ID
目前我有以下代码来删除表行:
var remove = document.getElementById(dataID); (this is the id of an object in the row I wish to hide)
if(remove!=null){
var v = remove.parentNode.parentNode.rowIndex;
document.getElementById(tid).deleteRow(v); (tid is the table id, not the row id)
}
但是,我只想隐藏它,而不是删除它。有什么好方法可以做到这一点?
另外,将来,我将要根据用户请求"取消隐藏"它,那么如何检查它是否已被隐藏?if(remove!=null) 是检查是否已经删除了一行的内容,所以我需要类似的东西。
谢谢你的时间。
document.getElementById(tid).children[dataID].style.display = 'none';
您可能需要-1
dataID
block
再次显示它(或inline
或任何它最初是什么,div
它是block
)。
JQuery:
$('#'+tid+':nth-child('+dataID+')').hide();
我自己的方法,在普通的JavaScript中:
function toggleRow(settings) {
// if there's no settings, we can do nothing, so return false:
if (!settings) {
return false;
}
// otherwise, if we have an origin,
// and that origin has a nodeType of 1 (so is an element-node):
else if (settings.origin && settings.origin.nodeType) {
// moving up through the ancestors of the origin, until
// we find a 'tr' element-node:
while (settings.origin.tagName.toLowerCase() !== 'tr') {
settings.origin = settings.origin.parentNode;
}
// if that tr element-node is hidden, we show it,
// otherwise we hide it:
settings.origin.style.display = settings.origin.style.display == 'none' ? 'table-row' : 'none';
}
// a simple test to see if we have an array, in the settings.arrayOf object,
// and that we have a relevant table to act upon:
else if ('join' in settings.arrayOf && settings.table) {
// iterate through that array:
for (var i = 0, len = settings.arrayOf.length; i < len; i++) {
// toggle the rows, of the indices supplied:
toggleRow({
origin: table.getElementsByTagName('tr')[parseInt(settings.arrayOf[i], 10)]
});
}
}
}
// you need an up-to-date browser (you could use 'document.getElementById()',
// but I'm also using 'addEventListener()', so it makes little difference:
var table = document.querySelector('#demo'),
button = document.querySelector('#toggle');
// binding a click event-handler to the 'table' element-node:
table.addEventListener('click', function (e) {
// caching the e.target node:
var t = e.target;
// making sure the element is a button, and has the class 'removeRow':
if (t.tagName.toLowerCase() === 'button' && t.classList.contains('removeRow')) {
// calling the function, setting the 'origin' property of the object:
toggleRow({
origin: t
});
}
});
// binding click-handler to the button:
button.addEventListener('click', function () {
// calling the function, setting the 'arrayOf' and 'table' properties:
toggleRow({
'arrayOf': document.querySelector('#input').value.split(/'s+/) || false,
'table': table
});
});
JS小提琴演示。
引用:
-
document.querySelector()
. -
e.target.addEventListener()
. -
Node.nodeType
. -
String.split()
.
正如你要求一个jQuery解决方案...怎么样
var $remove = $('#' + dataID);
if ($remove) {
$remove.closest('tr').closest().hide();
}
?
相关文章:
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- JavaScript-在手机上不工作的情况下,在外部单击时隐藏元素
- 如何使用JavaScript在没有html dom的情况下隐藏html元素
- 默认情况下,在展开和折叠表时隐藏行
- 是否可以在不重新渲染的情况下显示/隐藏父对象中的元素
- 如何在应用切换点击功能之前,默认情况下在页面加载时隐藏带有特定类的ul-li
- 默认情况下,在ipython笔记本中隐藏输入单元格
- 如何在没有按钮的情况下实现显示隐藏JavaScript
- 如何在没有科尔多瓦插件的情况下隐藏软键盘,也不会失去焦点
- 允许在隐藏溢出的情况下进行触摸滚动
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- 在没有数据源结果的情况下隐藏BIRT中的网格/表
- jQuery默认情况下不隐藏元素
- 如何在没有safari的情况下为浏览器隐藏视频元素
- 在表单未传递的情况下使用.html()动态插入隐藏文本字段
- 如何在不隐藏键盘的情况下清除输入搜索?(手机)
- Jquery一般情况下,在“是/否”单选按钮中显示/隐藏分区内的分区
- 如何在没有代码隐藏访问权限的情况下将自定义 HTML 属性添加到 ASP.NET 标签
- 如何切换回复、显示和隐藏回复.默认情况下,它们应该被隐藏
- Rail对默认情况下隐藏在表单中的属性进行建模