Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用jQuery替换DOM文本_Jquery_Replace - Fatal编程技术网

用jQuery替换DOM文本

用jQuery替换DOM文本,jquery,replace,Jquery,Replace,我有一个基本的DOM“大纲”,其中包含一个表,其中包含以下伪数据: <table> <tr> <td>name</td> <td>phone</td> </tr> </table> 两者都不起作用。脚本克隆并显示了行,但是我有30行“name”和“phone” 我使用Firebug的console.log进行调试,我确信它正确地从JSON获取了数据,并加载到“particip

我有一个基本的DOM“大纲”,其中包含一个表,其中包含以下伪数据:

<table>
  <tr>
    <td>name</td>
    <td>phone</td>
  </tr>
</table>
两者都不起作用。脚本克隆并显示了行,但是我有30行“name”和“phone”


我使用Firebug的console.log进行调试,我确信它正确地从JSON获取了数据,并加载到“participant”参数中。我做错了什么?

您使用的是标记中不存在的
td.name
.name
表示一个类,因此您需要像:
这样的标记才能工作

如果您使用它作为一个虚拟行并替换这两个td,我会为每个类提供一个类,比如
来使用
newRow.find(“td.name”)
或者使用类似的东西:
newRow.find(“td:first”)
newRow.find(“td:last”)
find('td.name')
查找带有
class=“name”的
td
,您没有。我建议将其添加到HTML中,为jQuery创建一个钩子来更新文本。特别是如果你在页面上有几个


另外,你是如何设置新行的?

如果我理解你想做什么,第二个版本几乎是正确的。注意选择器前面的冒号和内部单引号的使用

newRow.find("td:contains(name)").text(participant.name);

newRow.find("td:contains(phone)").text(participant.homePhone);
newRow.find(":contains('name')").text(participant.name);
newRow.find(":contains('phone')").text(participant.homePhone);
更好的方法是提供模板类并使用它们,或者仅仅依靠定位

定位:

newRow.find('td:first-child')
      .text(participant.name)
      .next()
      .text(participant.homePhone);
课程:

newRow.find( 'td.name' ).text( participant.name );
newRow.find( 'td.phone' ).text( participant.homePhone );
这一变化的必要性:

<table> 
  <tr> 
    <td class="name">name</td> 
    <td class="phone">phone</td> 
  </tr> 
</table>

名称
电话

真的需要看看脚本的其余部分/markupNow,如果参与者的名字是Stephone Andrews会发生什么?呵呵。我同意使用类名更有意义,但我试图纠正他提供的逻辑&而不是将他重定向到更好的解决方案……谢谢。我选择这个作为答案,尽管几乎每个人都告诉我使用类。当然“它现在起作用了。”嘉莉——注意上课是一个很好的方法。我不会使用
contains
方法,因为如果某人的姓名包含字符串
phone
,则可能会出现问题。DOM相对机制运行良好,但对表结构中的更改很敏感。
<table> 
  <tr> 
    <td class="name">name</td> 
    <td class="phone">phone</td> 
  </tr> 
</table>