简体   繁体   English

纯JavaScript-在DOM元素中编辑文本而不接触子元素

[英]Pure JavaScript - edit text in DOM element without touching child elements

I'm currently trying to edit the text of all child elements of a single parent element. 我目前正在尝试编辑单个父元素的所有子元素的文本。 The structure is something along the lines of: 该结构类似于以下内容:

<section id=emoji>
        <h1>Emojies</h1>
        <p id=ea1>:-)</p>
        <p id=ea2>(TM)</p>
        <p id=ea3>Trademark(TM) <span>:-</span>)</p>
        <p id=ea4>Do you &lt;3 me?</p>
        <p id=ea5>:-):-):-)&lt;3&lt;3&lt;3</p>
</section>

As you can see, the third paragraph tag has a span tag. 如您所见,第三段标记具有span标记。 I have a function that will take a given string and convert all instances of an emoji symbol to a specified emoji within the emojify function. 我有一个函数,它将使用给定的字符串并将表情符号的所有实例转换为emojify函数中的指定表情。 I want to dynamically do the same to these elements within the section . 我想动态地对本节中的这些元素执行相同的操作。 The problem is I don't want to edit the span , and at the minute, incorporating the span tag will generate a smiley face, which I don't want to do. 问题是我不想编辑span ,并且在一分钟之内,合并span标签会生成一张笑脸,这是我不想做的。

My emoji function looks like this: 我的表情符号功能如下所示:

function emojify(str) {
    let emojies = [];
    emojies['(TM)'] = '™️';
    emojies['<3'] = '❤️';
    emojies[':-)'] = '😀';

    emojies.forEach(function(el) {
        if (str.indexOf(el) > -1) {
            str = str.split(el).join(emojies[el]);
        }
    });

    return str;
}

This is my current function for trying to dynamically do this: 这是我目前尝试动态执行的功能:

function pageEmojify(selector) {
    let element = document.querySelector(selector);
    element.childNodes.forEach(function(el) {
        el.textContent = emojify(el.textContent);
    });
}

It works, I just need it to not include the span tag in any conversion. 它有效,我只需要在任何转换中不包含span标签即可。

I have looked at various questions here on StackOverflow however all seem to use jQuery which I don't want to use for this particular problem. 我在StackOverflow上查看了各种问题,但是似乎所有问题都使用了jQuery,但我不想将其用于该特定问题。

For example: How to only change the text in a DOM element without replacing any child elements 例如: 如何仅更改DOM元素中的文本而不替换任何子元素

How can I edit my pageEmojify function to do this? 我如何编辑我的pageEmojify函数来做到这一点?

 function emojify(str) { let emojies = []; emojies['(TM)'] = '™️'; emojies['<3'] = '❤️'; emojies[':-)'] = '😀'; for (let el of Object.keys(emojies)) { if (str && str.indexOf(el) > -1) { str = str.split(el).join(emojies[el]); } } return str; } function pageEmojify(selector) { let element = document.querySelector(selector); element.childNodes.forEach(function(el) { if (el.childNodes) { el.childNodes.forEach(function(elm) { elm.nodeValue = emojify(elm.nodeValue) }) } else el.textContent = emojify(el.textContent); }); } pageEmojify('section'); 
 <section id='emoji'> <h1>Emojies</h1> <p id='ea1'>:-)</p> <p id='ea2'>(TM)</p> <p id='ea3'>Trademark(TM) <span>:-</span>)<span>:-</span>)</p> <p id='ea4'>Do you &lt;3 me?</p> <p id='ea5'>:-):-):-)&lt;3&lt;3&lt;3</p> </section> 

Check for nodeValue of node to get that value for the given element. 检查节点的nodeValue以获得给定元素的值。 If any other nodes are there loop through that array to get all other node content. 如果还有其他节点,则遍历该数组以获取所有其他节点的内容。

If you check in the properties of your page (in chrome dev tool, go to element, click on an element and go to view properties in the right side), you can see that there is a 'text' node in each of you 如果您检入页面的属性(在chrome dev工具中,转到元素,单击一个元素,然后查看右侧的属性),则可以看到每个页面中都有一个“文本”节点

element . 元素。 In case of the 如果是

element whoc has span in it, it will show that there are 3 element - text, span and a text - in it. 元素whoc中有span,它将显示其中包含3个元素-文本,span和一个文本。 You can modiy you function to process only teh text nodes in it. 您可以修改其功能以仅处理其中的文本节点。 It would be like 就像

function pageEmojify(selector) {
    let element = document.querySelector(selector);
    element.childNodes.forEach(function(el) {
        ## get child nodes of e
        ## if teh childnode is of type text, then get innertext and pass it to emojify  
    });
}

I m giving the pseudo code here, as im not an expert in js functions. 我在这里提供伪代码,因为我不是js函数专家。

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

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