jQuery 使用包含其他元素的 td 替换 td 中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13816944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace text inside td using jQuery having td containing other elements
提问by ????
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
我只想更改文本8: Tap on APN and Enter <B>www</B>.
而不影响隐藏字段
I am trying jQuery but not finding the solution
我正在尝试 jQuery 但没有找到解决方案
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());
});
}
回答by Ja?ck
Using text nodes in jquery is a particularly delicate endeavour and most operations are made to skip them altogether.
在 jquery 中使用文本节点是一项特别微妙的工作,大多数操作都是为了完全跳过它们。
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:
与其小心翼翼地避免错误的节点,不如将您需要替换的任何内容包装在一个<span>
示例中:
<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.');
回答by undefined
$('#demoTable td').contents().each(function() {
if (this.nodeType === 3) {
this.textContent
? this.textContent = 'The text has been '
: this.innerText = 'The text has been '
} else {
this.innerHTML = 'changed';
return false;
}
})
回答by adeneo
Remove the textnode, and replace the <b>
tag with whatever you need without ever touching the inputs :
删除文本节点,并在<b>
不接触输入的情况下用您需要的任何内容替换标签:
$('#demoTable').find('tr > td').contents().filter(function() {
return this.nodeType===3;
}).remove().end().end()
.find('b').replaceWith($('<span />', {text: 'Hello Kitty'}));
回答by serv-inc
A bit late to the party, but JQuery change inner text but preserve htmlhas at least one approach not mentioned here:
聚会有点晚了,但JQuery 更改内部文本但保留 html至少有一种此处未提及的方法:
var $td = $("#demoTable td");
$td.html($td.html().replace('Tap on APN and Enter', 'new text'));
Without fixing the text, you could use (snother)[https://stackoverflow.com/a/37828788/1587329]:
在不修复文本的情况下,您可以使用 (snother)[ https://stackoverflow.com/a/37828788/1587329]:
var $a = $('#demoTable td'); var inner = ''; $a.children.html().each(function() { inner = inner + this.outerHTML; }); $a.html('New text' + inner);
var $a = $('#demoTable td'); var inner = ''; $a.children.html().each(function() { inner = inner + this.outerHTML; }); $a.html('New text' + inner);
回答by webnoob
How about:
怎么样:
function changeText() {
$("#demoTable td").each(function () {
$(this).html().replace("8: Tap on APN and Enter <B>www</B>", "");
}
}
回答by Vivek S
Wrap your to be deleted contents within a ptag, then you can do something like this:
将要删除的内容包裹在 ptag 中,然后您可以执行以下操作:
$(function(){
$("td").click(function(){ console.log($("td").find("p"));
$("td").find("p").remove(); });
});
FIDDLE DEMO: http://jsfiddle.net/y3p2F/
小提琴演示:http: //jsfiddle.net/y3p2F/