Javascript/html:如何存储div,并在以后隐藏其子项



>我正在存储一个被选中的div

var selectedCell = null;
    $(".selectableBox").on('click', function (event) {
            selectedCell = $(this);
        }

稍后我想隐藏一个可选单元格的孩子名称可选单元格儿童

$('#deleteConfirmed').on('click', function (event) {
    selectedCellList.($'selectableCellChild').hide();
});

如何正确隐藏此子div? 我知道上面例子的语法不正确,我已经尝试了很多方法,包括使用 selectedCellList 的 children() 和 next() 方法

selectedCellList.find('{selectableCellChild}').hide();

其中selectableCellChild是单元格实际选择器的占位符。

我已经尝试了很多方法,包括使用child()和next()

  • children - 仅遍历一层深度。
  • find - 遍历所有 DOM 级别。
  • next选择下一个直系同级。

对于第二部分,这是您想要的:

$('#deleteConfirmed').on('click', function (event) {
    $(selectedCellList).find('.selectableCellChild').hide();
});

如果我理解正确,您正在尝试隐藏单击的div 的子项。 尝试如下,

var selectedCell = null;
$(".selectableBox").on('click', function (event) {
   selectedCell = $(this);
}); //Your missed );
$('#deleteConfirmed').on('click', function (event) {
   //v-- Changed from selectedCellList to selectedCell as this is the clicked div.
   selectedCell.find('.selectableCellChild').hide(); 
   //assuming selectableCellChild-^ is class of child elements in the clicked div
});

使用 .find

selectedCellList.find('selectableCellChild').hide(); // I hope selectableCellChild isn't your real selector, it won't work

此外,在声明变量时,请使其成为 jQuery 对象,因为您打算在其中存储一个 jquery 对象以避免未定义的方法错误。

var selectedCell = $();

最新更新