0

目前我有这个代码来删除一个表行:

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) 是检查是否已经删除了一行,所以我需要类似的东西。

感谢您的时间。

4

3 回答 3

2

document.getElementById(tid).children[dataID].style.display = 'none';

您可能需要-1dataID

block再次显示它(inline或任何它原来的样子,对于 a divit's block)。

查询:

$('#'+tid+':nth-child('+dataID+')').hide();

于 2013-10-01T21:25:36.427 回答
2

我自己的方法,在纯 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 小提琴演示

参考:

于 2013-10-01T21:57:18.813 回答
1

正如你所要求的 jQuery 解决方案......怎么样

var $remove = $('#' + dataID);

if ($remove) {
    $remove.closest('tr').closest().hide();
}

?

于 2013-10-01T21:57:41.853 回答