如果表单元格包含包含字符串的注释节点,请选择该单元格

时间:2014-04-29 18:34:49

标签: javascript jquery html-table

我需要测试具有属性section-name = section2的任何行的第二个单元格以及包含' FieldInternalName="BaseLineEUI"的第二个单元格'在评论节点 - 是否为空?我在此之后就放弃了:

jQ("tr[section-name='Section2']").each(function(i){
$(this).children('td').slice(0,2).
 //how do I test if the comment contains a string here?

})

<tr style="display: block;" section-name="Section2">
      <TD class=ms-formlabel vAlign=top width=165 noWrap>
      <H3 class=ms-standardheader>
      <A name=SPBookmark_ClimateZoneASHRAEIECC></A>ClimateZoneASHRAEIECC</H3>
      </TD>
      <TD id=SPFieldText class=ms-formbody vAlign=top width=450>
      <!-- FieldName="ClimateZoneASHRAEIECC"  
          FieldInternalName="ClimateZoneASHRAEIECC" FieldType="SPFieldText" -->&nbsp; 
       </TD>    
    </tr>

    <tr style="display: block;" section-name="Section2">
      <TD class=ms-formlabel vAlign=top width=165 noWrap>
      <H3 class=ms-standardheader>
      <A name=SPBookmark_BaselineEUI></A>BaselineEUI</H3>
     </TD>
      <TD id=SPFieldNumber class=ms-formbody vAlign=top width=450>
      <!-- FieldName="BaselineEUI"  FieldInternalName="BaselineEUI" 
       FieldType="SPFieldNumber" -->&nbsp; 
     </TD>
    </tr>

1 个答案:

答案 0 :(得分:1)

我建议(鉴于要求和丑陋, hideous HTML):

// selects those 'td' elements that are the second child (:nth-child(2))
// of their parent 'tr' element with a 'section-name' attribute that is
// equal to 'Section2', and filters that collection:
$("tr[section-name='Section2'] td:nth-child(2)").filter(function(){

    // we retain only those 'td' elements with at least one childNode
    // that is a comment-node (nodeType === 8) and contains the string
    // supplied to 'indexOf()'
    return $(this).contents().filter(function(){
        return this.nodeType === 8 && this.nodeValue.indexOf('FieldInternalName="BaselineEUI"') > -1;
    // if there is a length (IE there was more than zero matching
    // nodes found) we retain the 'td' element we're filtering
    }).length;
// moving to the previous 'td' element, and using 'css()' to color its text red:
}).prev('td').css('color', 'red');

JS Fiddle demo

参考文献: