我有一个表tr
和td
。我的td
有一个div
作为它的孩子。其中一个div
有id="question"
,另一个有id="answer"
。我想在遍历我的td
时,div
有id="answer"
被隐藏。当页面加载时,只会出现问题div
。这里是html的结构jQuery中的隐藏元素表
<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
的是推栈上只应该隐藏,但是这不是真正的输出。
哦,那是错误的,对不起 – Basit 2012-01-17 12:21:27
不要使用ID。因为我看到这种结构可以重复。 ID是整个文档的唯一标识符。而是使用类名。 – footy 2012-01-17 12:22:56