开发者

changing and hiding parts of a table

I've got a html table with td class-formlabel and text (HP)Hello

  1. Why doesn't this replace the text?

    $(this).text().replace('(HP)','');
    
  2. Why does this remove formating if this is $("td.ms-formlabel").each(function(){

    $(this).text("hello");
    
  3. How can hide the TR ro开发者_如何学运维ws for a match given the rendered html above and the below code? (Note I need to use the below condition and loop)

    $("td.ms-formlabel").each(function(){
    
    if($(this).text().match('(HP)')){
       // what code here to hide the entire TR row ?
    }
    

Thanks.


  1. $(this).text().replace('(HP)',''); does replace the text, but you don't do anything with it - try:

    var text = $(this).text().replace('(HP)','');
    $(this).text(text);
    
  2. It shouldn't remove and formatting unless you're applying it with tags inside.ms-formlabel

    <td class="ms-formlabel"><b>Bold</b></td>
    

    will become

    <td class="ms-formlabel">Hello</td>
    
  3. You can use:

    $(this).closest('tr').hide();
    


  1. .text() returns a string. To modify and replace the text, you have to set it back again. It's kind of the equivalent to the fact that x + 5 doesn't change x. You have to type x = x + 5 In this case, the code would be this:

    $(this).text($(this).text().replace('(HP)',''));
    
  2. I don't quite understand this question. If you're trying to set some HTML styling, use the .html() function instead.

  3. Try this:

    if ($(this).text().match('(HP)')) {
        $(this).closest('tr').hide();
    }
    


A couple of things:

  • You should use :contains
  • Text will strip formatting, you should use .html()
  • To specify HTML (or text for that matter), you put the new content within the brackets, i.e. element.text(content)

So here is an example...

$("td.ms-formlabel:contains('(HP')").each(function(){
    var $this = $(this);
    $this.html($this.html().replace('(HP)',''));
    $this.closest('tr').hide();
}

I think that's what you want. The only thing that I might be wrong with is which row you want to hide... your wording is kind of confusing...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜