用td包含其他元素替换td中的文本 [英] Replace text inside td using jQuery having td containing other elements

查看:160
本文介绍了用td包含其他元素替换td中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格如下:

 < table id ='demoTable'> 
< tr>
< td> 8:点按APN并输入< B> www< / B> ;.
< INPUT id = h250000000000000109743 class = hid1 value =26,222,98,10,50000000000000109744,T,〜25,221,99,10,T,wwwtype =hidden>
< / td>
< / tr>
< / table>

我只想更改文字 8:点按APN并输入< ; B> www< / B>。

在不影响隐藏字段的情况下

找到解决方案

  function changeText(){
$(#demoTable td)。each(function() {
for(var i = 0; i <$(this).children.length; i ++){
alert($(this).children(i).val());

// alert($(this).html());
// $(this).text(hello);
// alert($(this ).html());
});


解决方案

在jquery中使用文本节点是一个特别微妙的努力和大多数操作是完全跳过它们的。

为什么不把你需要的任何东西在< span> 内替换,例如:

 < td> ;< span class =replaceme> 8:点按APN,然后输入< B> www< / B>< / span>< / td> 

然后:

  $('。replaceme')。html('Whatever< b> HTML< / b>你想在这里。'); 


My table is as follows:

<table id='demoTable'>
   <tr>
       <td>8: Tap on APN and Enter <B>www</B>.
           <INPUT id=h150000000000000109743 class=hid value="test value" type=hidden>
           <INPUT id=h250000000000000109743 class=hid1 value="26,222,98,10,50000000000000109744,T,~25,221,99,10,,T,www" type="hidden">
       </td>
   </tr>
</table>

I want to change the text only 8: Tap on APN and Enter <B>www</B>.
without affecting the hidden fields

I am trying jQuery but not finding the solution

function changeText() {
    $("#demoTable td").each(function () {
        for (var i = 0; i < $(this).children.length; i++) {
            alert($(this).children(i).val());
        }
        // alert($(this).html());
        // $(this).text("hello");
        // alert($(this).html());
    });
}

解决方案

Using text nodes in jquery is a particularly delicate endeavour and most operations are made to skip them altogether.

Instead of going through the trouble of carefully avoiding the wrong nodes, why not just wrap whatever you need to replace inside a <span> for instance:

<td><span class="replaceme">8: Tap on APN and Enter <B>www</B>.</span></td>

Then:

$('.replaceme').html('Whatever <b>HTML</b> you want here.');

这篇关于用td包含其他元素替换td中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆