简体   繁体   English

如何用子元素替换元素内的部分文本?

[英]How do I replace part of text inside an element with children?

I have this HTML: 我有这个HTML:

<div class="test">Orange <span class="is">is</span> my favorite color.</div>

I only want to replace Orange (and the space after it). 我只想替换Orange (及其后的空格)。 Using .text() won't work because it selects the whole strong. 使用.text()无效,因为它会选择整个强度。

How do I do this? 我该怎么做呢?

This is harder to do with jQuery than with the native DOM, so if you're using jQuery, you're going to have to convert the element to the native DOM using jQuery's array indexing. 使用jQuery比使用本机DOM更难,因此,如果使用jQuery,则必须使用jQuery的数组索引将元素转换为本机DOM。

Basically, what we need to do is change the first text node. 基本上,我们需要做的是更改第一个文本节点。 jQuery isn't really good with text nodes, which is why we use the DOM here: jQuery在文本节点上并不是很好,这就是为什么我们在这里使用DOM的原因:

 //This gets the div.test element using jQuery: var testDiv = $("div.test"); function changeColor(color) { /* The following function changes the value of the first text node within testDiv: */ //This converts testDiv from a jQuery element to a regular DOM element. This is because jQuery isn't really meant to be handling stuff like text nodes, so doing this with the regular DOM will just be much easier. var domElement = testDiv[0]; //Now, just change the value of the first text node: domElement.childNodes[0].nodeValue = color; } //Now, as you can see if you click div.test, we can replace the color mentioned in div.test using changeColor(): testDiv.click(function() { changeColor("Blue "); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test">Orange <span class="is">is</span> my favorite color.</div> 

You should use native DOM methods. 您应该使用本机DOM方法。 In your case the simples thing is just change nodeValue property of the first childNode element (which you know is going to be TextNode element): 在您的情况下,简单的事情就是更改第一个childNode元素的nodeValue属性(您知道它将是TextNode元素):

 var el = $('.test'); el[0].childNodes[0].nodeValue = 'Green '; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test">Orange <span class="is">is</span> my favorite color.</div> 

or if you want you can grab text node with jQuery's $.fn.contents method: 或者,如果您愿意,可以使用jQuery的$ .fn.contents方法获取文本节点:

 var el = $('.test'); el.contents()[0].nodeValue = 'Green '; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test">Orange <span class="is">is</span> my favorite color.</div> 

If you are using jquery, here is the solution 如果您使用的是jQuery,这是解决方案

    $('.test').html(function(){
        return $(this).html().replace('Orange ','');
    });

Find the fiddle here: https://jsfiddle.net/MasoomS/xff6dw1t/ 在这里找到小提琴: https : //jsfiddle.net/MasoomS/xff6dw1t/

Used data attribute to refine the code and to use it for multiple strings 使用data attribute来优化代码并将其用于多个字符串

HTML: HTML:

 <div class="test" data-replace="Orange ">Orange <span class="is">is</span> my favorite color.</div>
 <div class="test" data-replace="Green ">Green <span class="is">is</span> my next favorite color.</div>

JQUERY: JQUERY:

    $('.test').html(function(){
        return $(this).html().replace($(this).data('replace'),'');
    });
document.getElementsByClassName("test")[0].childNodes[0].nodeValue = "Black";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM