简体   繁体   English

如何输入多个跨度标签替换 function

[英]How to enter multiple span tags in replace function

I have a sentence and I am trying to replace globally one word by an under-line of equal space.我有一个句子,我试图用一个相等空格的下划线全局替换一个单词。 That is:那是:

  • Example: This is my first text, this is my second text and this is my third text.示例:这是我的第一个文本,这是我的第二个文本,这是我的第三个文本。
  • NewExample: This is my first ____, this is my second ____ and this is my third ____.新示例:这是我的第一个 ____,这是我的第二个 ____,这是我的第三个 ____。

My question is related with this question How to change color of a word in a sentence by clicking it on PC or by touching it on Android and the excellent answer of @terrymorse.我的问题与这个问题有关如何通过在 PC 上单击或在 Android 上触摸它来更改句子中单词的颜色以及@terrymorse的出色回答。 So, I want to use somehow the following in the replace function:所以,我想以某种方式在替换 function 中使用以下内容:

//  span.style.color = 'yellow';
//  span.style.width = span.getBoundingClientRect().width + 'px';
//  span.innerHTML = '_';
//  span.style.borderBottom = '2px solid yellow';

This is my full example but only color works.这是我的完整示例,但只有颜色有效。 Could you please someone help me and tell me what I am doing wrong with the rest of them?您能否请人帮助我并告诉我他们的 rest 做错了什么?

<div id='Example'>This is my first text, this is my second text and this is my third text.</div>
<div id='Split'>text</div>
<div id='NewExample'></div>
    
fReplace(Example, Split)

function fReplace(Example, Split) {
    v1 = Example.innerText;
    function myFunction(word) {
        var reg = new RegExp(word, "g");
    
        v1 = v1.replace(reg, `<span style = "color: yellow; word.borderBottom: 2px solid yellow; width: word.getBoundingClientRect().width + 'px'; word.innerHTML: '_'">${word}</span>`)
    }
    Split.forEach(myFunction);
    document.getElementById('NewExample').innerHTML = v1;   
}

You can create a function that replaces the target text with a span element.您可以创建一个 function 用span元素替换目标文本。 The span element will be used to display the underline. span元素将用于显示下划线。 I created a function named replaceTextWithUnderline in the code snippets below that demonstrates the expected behavior.我在下面的代码片段中创建了一个名为replaceTextWithUnderline的 function 来演示预期的行为。

I thought of two different approaches to display the underline.我想到了两种不同的方法来显示下划线。 Hopefully, one of these approaches fits your use case:希望其中一种方法适合您的用例:



Approach 1: Making the text color transparent and wrapping the target text in <span> tags方法一:使文本颜色透明并将目标文本包裹在<span>标签中

This approach is straightforward.这种方法很简单。 It involves replacing the target text with the target text wrapped inside of <span> tags.它涉及用包裹在<span>标记内的目标文本替换目标文本。

 function replaceTextWithUnderline(words, targetText = 'text') { const regex = RegExp(targetText, 'gi') const styles = ` color: transparent; border-bottom: 2px solid yellow; ` return words.replace(regex, `<span style="${styles}">${targetText}</span>`) } function fReplace() { const exampleEl = document.getElementById('Example') const newExampleEl = document.getElementById('NewExample') newExampleEl.innerHTML = replaceTextWithUnderline(exampleEl.innerText) } fReplace()
 <div id='Example'>This is my first text, this is my second text and this is my third text.</div> <div id='NewExample'></div>

Positive aspects to this approach are that the underline's width will be equal to the target text's width and that you can achieve the same result using classes and CSS.这种方法的积极方面是下划线的宽度将等于目标文本的宽度,并且您可以使用类和 CSS 获得相同的结果。 For example:例如:

<!-- Generated HTML from Javascript -->
<span class="underline">target text</span>
/* Accompanying styles for generated HTML */
.underline {
  color: transparent;
  border-bottom: 2px solid yellow;
}

A negative aspect is that this may not be a solution if you do not want any text inside of the <span> tags.不利的方面是,如果您不希望<span>标记内有任何文本,这可能不是解决方案。



Approach 2: Using ch to define the underline's width方法二:使用ch定义下划线的宽度

If you do not want any text inside of the <span> tags, you can opt to using the following CSS:如果您不希望<span>标记内有任何文本,您可以选择使用以下 CSS:

/* To apply a width, we need to change the display from inline to inline-block. */
display: inline-block;

/* <number> represents the number of characters in the target text */
width: <number>ch; 

 function createUnderlineStyles(numberOfChars) { return ` display: inline-block; width: ${numberOfChars}ch; border-bottom: 2px solid yellow; ` } function replaceTextWithUnderline(words, targetText = 'text') { const regex = RegExp(targetText, 'gi') const styles = createUnderlineStyles(targetText.length) return words.replace(regex, `<span style="${styles}"></span>`) } function fReplace() { const exampleEl = document.getElementById('Example') const newExampleEl = document.getElementById('NewExample') newExampleEl.innerHTML = replaceTextWithUnderline(exampleEl.innerText) } fReplace()
 <div id='Example'>This is my first text, this is my second text and this is my third text.</div> <div id='NewExample'></div>

This utilizes the ch unit .这利用了ch单元 From MDN's documentation, the ch unit is:从 MDN 的文档中, ch单位是:

The advance measure (width) of the glyph "0" of the element's font.元素字体的字形“0”的提前量度(宽度)。


This approach can be used if you do not want any text inside of the <span> tags.如果您不希望<span>标记内有任何文本,则可以使用此方法。 However, downsides to this approach are that the underline's width will not exactly match the target text's width and you will most likely not be able to implement this with CSS due to the nature of the number of characters being dynamic.但是,这种方法的缺点是下划线的宽度与目标文本的宽度不完全匹配,并且由于字符数的性质是动态的,您很可能无法使用 CSS 实现这一点。

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

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