jQuery 如何仅更改元素中的文本节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9956388/
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
How to change only text node in element
提问by MikDiet
I have next html:
我有下一个 html:
<label for="user_name">
<abbr title="required">*</abbr>
Name
</label>
And I want to change label caption to Titlewith jquery. So I do
我想Title用 jquery将标签标题更改为。所以我做
$('label[for=user_name]').html('Title')
And it replace all inner html (with abbrtag)
并替换所有内部 html(带abbr标签)
So, what the easiest way to replace only Name?
那么,什么最简单的方法只更换Name?
回答by charlietfl
If you use contents()method it will also return text nodes. Since jQuery doesn't have text node methods, convert last node to a DOM node
如果您使用contents()方法,它还将返回文本节点。由于 jQuery 没有文本节点方法,因此将最后一个节点转换为 DOM 节点
$('label[for="user_name"]').contents().last()[0].textContent='Title';
回答by Dom
Sorry for the late reply... But here is a way to do so using only jQuery:
抱歉回复晚了……但这里有一种方法可以只使用 jQuery:
$('label').contents().last().replaceWith('Title');
回答by Code Maverick
It may not be the prettiest way, but this works:
var $label = $('label[for=user_name]');
$label.html($label.html().replace("Name", "Title"));
回答by Jignesh Rajput
you can use replaceaccomplish this
你可以使用replace完成这个
var html = $('label[for=user_name]').html().replace('Name','Testing');
$('label[for=user_name]').html(html);
check it : http://jsfiddle.net/DyzMJ/
回答by m90
You can select only the abbrelement, store it, and then replace the whole content with the stored element plus the changed caption:
您可以只选择abbr元素,存储它,然后用存储的元素加上更改的标题替换整个内容:
?$('label[for="user_name"]').each(function(){
var a = $(this).children('abbr');
$(this).html(a).append('Title');
});
See this fiddle?
看到这个小提琴了吗?
回答by user2525982
This is the solution that worked for the most browsers
这是适用于大多数浏览器的解决方案
$('label[for="user_name"]').contents().last()[0].nodeValue = 'Title';
This one came close but gave issues in ie8 since textContent is not supported
这个接近但在 ie8 中出现问题,因为不支持 textContent
$('label[for="user_name"]').contents().last()[0].textContent='Title';
回答by Erik255
Evans solution added to jquery fn to make it's use comfortable:
将 Evans 解决方案添加到 jquery fn 以使其使用舒适:
// get/change node content not children
jQuery.fn.content = function( n ){
var o = $(this).clone();
var c = o.children().remove();
if (typeof n === "string" ){
o.html(n);
$(this).html(c).append(n);
}
return o.html();
}
Usage :$('myselector').content('NewContentString');
用法 :$('myselector').content('NewContentString');
回答by Seif Tamallah
if you are manipulating more than 1 label you can select each label and replace text with jquery:
如果您要操作 1 个以上的标签,您可以选择每个标签并用 jquery 替换文本:
$('label[for="user_name"]').contents().last().replaceWith("Title");
and for the second label :
对于第二个标签:
$('label[for="user_lastname"]').contents().last().replaceWith("Title2");
and so on ...
等等 ...

