在< td>上使用Javascript替换元素 [英] Using Javascript replace on a <td> element

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

问题描述

我已经在与我的查询有关的网站(和网络)上进行了大量搜索,但无法获得直接答案.我需要从动态创建的表中删除其他"一词.我正在使用的代码如下:

I have done quite a bit of searching on the site (and the web) in relation to my query but cannot get a straight answer. I need to remove the word "Other" from a dynamically created table. The code I am using is as follows:

<td id="testclass">Other Option</td>

var str = document.getElementById('test').innerHTML;
var text = str.replace("Other", "");
document.getElementById("test").innerHTML = text;

<td id="test">Other Option</td>

但是由于某种原因,它不适用于元素,但将适用于p,span,div等.

For some reason though, this does not work with the element but it will work with p, span, div etc.

有人能对此有所启发吗?我们已经尝试了一些jquery选项,并且还是一样.

Can anyone shine some light on this? We have tried some jquery options to and still the same thing.

这里是 jsfiddle

推荐答案

您丢失了整个表格:

<table>
    <tr>
         <td id="test">Other Option</td>
    </tr>
</table>

要说明:在没有表格的情况下,HTML中省略了<td>,而该元素的上下文没有其父元素.
由于您的TD的ID丢失了,因此在这种情况下,在DOM ready上的JS找不到该特定ID.

To explain : in absence of a table the <td> is omitted from your HTML, leaving that element's context without it's parent.
Since your TD has an ID which gets lost, JS in that case, on DOM ready cannot find this specific ID.

小提琴

这篇关于在&lt; td&gt;上使用Javascript替换元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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