changing and hiding parts of a table
I've got a html table with td class-formlabel and text (HP)Hello
Why doesn't this replace the text?
$(this).text().replace('(HP)','');
Why does this remove formating if
this
is$("td.ms-formlabel").each(function(){
$(this).text("hello");
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.
$(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);
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>
You can use:
$(this).closest('tr').hide();
.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 thatx + 5
doesn't changex
. You have to typex = x + 5
In this case, the code would be this:$(this).text($(this).text().replace('(HP)',''));
I don't quite understand this question. If you're trying to set some HTML styling, use the
.html()
function instead.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...
精彩评论