【发布时间】:2012-02-12 05:56:50
【问题描述】:
我有一张带有tr 和td 的桌子。我的td 有一个div 作为它的孩子。一个div 有id="question",另一个有id="answer"。我希望在遍历我的td 时隐藏具有id="answer" 的div。当页面加载时,才会出现问题div。这是html的结构
<td style="display: none;">
<div style="border-color: #000000;position: relative;float: right;margin-top: 2px;right: 12%;"> </div>
<div style="border-color: #000000;position: relative;float: right;margin-top: 2px;right:-2%;"> </div>
<div id="question">
<img id="faqGrid:0:j_idt77" height="10" width="480" src="/TDAP/faces/javax.faces.resource/spacer/dot_clear.gif?ln=primefaces&v=2.2.RC2">
<br>
<br>
<div id="answer">
<div class="horizontalline"></div>
</td>
我做了以下事情。但它隐藏了整个 td。
$('#faqGrid tr td').each(function(){
var $cells = $();
/**
*Gives you all children as an object array
* 0: div
* 1: div
* 2: div#question
* 3: img#faqGrid:0:j_idt77 /TDAP/fa...=2.2.RC2
* 4: br
* 5: br
* 6: div#answer
* 7: div.horizontalline
*/
var tdChildrenArray = $(this).children();
var divChildrenArray = $(this).children('div');
var elementStack;
$.each(divChildrenArray, function(index, value){
var $div = value;
if ($div.id) {
var $divId = $div.id;
if ($divId == 'answer') {
var colNum = $(this).cellIndex;
$cells = $cells.add($div);
} //end of if ($divId == 'answer')
} //end of if ($div.id)
}); //end of $.each(object, fn)
return $(this).pushStack($cells);
}).hide(); //end of $('#faqGrid tr td').each(fn)
我的想法是,推送堆栈上的div 应该隐藏,但这不是真正的输出。
【问题讨论】:
-
哦,打错了,对不起
-
不要使用 ID。正如我所见,这种结构可以重复。 ID 是整个文档的唯一标识符。而是使用类名。
标签: javascript jquery html-table jquery-selectors