Change text inside HTML element using Javascript or anything

改变在HTML元素的文本使用JavaScript或什么的

问题 (Question)

can some one please help me to change Text only that's inside HTML tags

for example, on my body page when page loaded it shows

<td>Hello</td>

and i want to change that text only it says "Hello" to "Hi" on ready function No onclick

another example, i know there's on javascript some how it's like, if inside td equals "Hello" then change it to "Hi".

if there's some solution by if...else function that will be better

Thanks.

有人可以请帮我只改变文本内部的HTML标签

例如,在我身上时,它显示页面加载的页面

<td>Hello</td>

我想把那只说“你好”文本“嗨”准备功能无onclick

另一个例子,我知道那里的javascript一些怎么那么像,如果在TD等于“你好”,然后更改为“嗨”。

如果有一些解决的其他功能,如……会更好

感谢

最佳答案 (Best Answer)

Using jQuery:

$(function(){
     $('#tdID').text('Hi');
});

Or with pre-condition:

$(function(){
    if($('#tdID').text() == 'Hello'){
        $('#tdID').text('Hi');
     }
});

UPDATE:

For this code to work, you must have your td to be nested within a tr which should be nested within a table:

<table>
    <tr>
        <td id="myID">Hello</td>
    </tr>
</table>

See live example here

Full code:

<html>
<head>
<title>jQuery text's attribute demo</title>
</head>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(function(){
    if($('#myID').text() == 'Hello'){
        $('#myID').text('Hi');
    }
});
</script>
<body>
<table>
    <tr>
        <td id="myID">Hello</td>
    </tr>
</table>
</body>
</html>

使用jQuery:

$(function(){
     $('#tdID').text('Hi');
});

或先决条件:

$(function(){
    if($('#tdID').text() == 'Hello'){
        $('#tdID').text('Hi');
     }
});

更新

此代码的工作,你必须有你的td被嵌套在一个tr这应该是嵌套在一个table

<table>
    <tr>
        <td id="myID">Hello</td>
    </tr>
</table>

看到活生生的例子在这里

完整的代码:

<html>
<head>
<title>jQuery text's attribute demo</title>
</head>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(function(){
    if($('#myID').text() == 'Hello'){
        $('#myID').text('Hi');
    }
});
</script>
<body>
<table>
    <tr>
        <td id="myID">Hello</td>
    </tr>
</table>
</body>
</html>

答案 (Answer) 2

HTML: <td id="myId">Hello</td>

JS:

 document.getElementById("myId").innerHTML="new text";

HTML<td id="myId">Hello</td>

JS:

 document.getElementById("myId").innerHTML="new text";

答案 (Answer) 3

This will change a from "Hello" to "Hi":

$('td').text('Hi');

If you give your element an id like so:

<td id="myTD">Hello</td>

Then you can change just the text:

$('#myTD').text('Hi');

This will check each <td> for "Hello" and change it to "Hi":

$( "td" ).each(function(index) {
  if( $(this).text() == "Hello" ) $(this).text('Hi');
});

这将改变从“你好”“你好”:

$('td').text('Hi');

如果你给你的元素的ID一样:

<td id="myTD">Hello</td>

然后你可以改变文本:

$('#myTD').text('Hi');

这将检查每个<td>“你好”改变为“嗨”:

$( "td" ).each(function(index) {
  if( $(this).text() == "Hello" ) $(this).text('Hi');
});

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/23450732