简体   繁体   English

粗体/斜体/下划线用户文本输入

[英]Bold/Italic/Underline User Text Input

I have a text input, I use DOM to get the text, I show the text.我有一个文本输入,我使用 DOM 来获取文本,我显示文本。 The problem is I want the user to be able to bold/italic/underline some words or part of the text.问题是我希望用户能够加粗/斜体/下划线某些单词或部分文本。 I was thinking about letting the user write BOLD/ITALIC/UNDERLINE around the text, search for the index of this words, and apply the changes with javascript.我正在考虑让用户在文本周围写 BOLD/ITALIC/UNDERLINE,搜索此单词的索引,然后使用 javascript 应用更改。 I can already do the first two steps, but i don't know how to apply the changes.我已经可以完成前两个步骤,但我不知道如何应用更改。
I search for ways to turn the text bold with javascript with document.createElement("B");我寻找使用 javascript 和document.createElement("B"); but using this method is complicated because it would be just specific words or sentences.但是使用这种方法很复杂,因为它只是特定的单词或句子。

var bold = {}; var bld = 0;
var str = 'string BOLD of BOLD the ITALIC input text ITALIC that i UNDERLINE get UNDERLINE using dom';
var show = document.createElement("P");
//Here I use .includes() and .matchAll() to get an array with all the indexes
var regexp = new RegExp("BOLD","g");
let arraybold = [...str.matchAll(regexp)].map(a => a.index);
//ex: arraybold[0] = 7 and arraybold[1] = 15. 
for(key in arraybold) {
    bld = bld + 1;
}
regexp = new RegExp("ITALIC","g");
let arrayitalic = [...str.matchAll(regexp)].map(a => a.index);
regexp = new RegExp("UNDERLINE","g");
let arrayunderline = [...str.matchAll(regexp)].map(a => a.index);
//Here I'm suppose to create the bold/italic/underline tags on the specific words. 
for(i = 1; i <= (bld/2); i++) {
var bold[i] = document.createElement("B");
bold[i].innerText = str.slice(arraybold[i]+4, arraybold[i+1]);
//str.replace(the part of the text with the bold tag that i create);
}
show.innerText = str;
document.body.appendChild(show);

Ps: I can't use innerHTML because i will use the dom later. Ps:我不能使用innerHTML,因为我稍后会使用dom。 I would prefer to do this with vanilla javascript.我更喜欢用香草 javascript 来做这件事。 The expected result:预期结果:

//<p>string <strong> of </strong> the <i> input text </i> that i <u> get</u> using dom</p>

edit: using let instead of var as suggested by @cpprookie编辑:@cpproookie 建议使用 let 而不是 var

Try to split string and fill an array with data which structure looks like { text: 'string', type: 'noStyle/BOLD/ITALIC/UNDERLINE' } , finally generate node based on type field.尝试拆分字符串并用结构看起来像{ text: 'string', type: 'noStyle/BOLD/ITALIC/UNDERLINE' }数据填充数组,最后根据类型字段生成节点。

  • First split and traverse whole string.首先拆分并遍历整个字符串。 Fill textList according to corresponding keywords(BOLD/ITALIC/UNDERLINE);根据对应的关键字(BOLD/ITALIC/UNDERLINE)填充textList;
var textList = [];
var boldText = '';
var italicText = '';
var underlineText = '';
var markAsBold = false;
var markAsItalic = false;
var markAsUnderline = false;

var str = 'string BOLD of BOLD the ITALIC input text ITALIC that i UNDERLINE get UNDERLINE using dom';

str.split(' ').forEach(s => {
  if (s === 'BOLD') {
     // match left BOLD
     if (!markAsBold) {
        markAsBold = true;
     } else {
        // match right BOLD and clear boldText;
       textList.push({
         text: boldText,
         style: 'BOLD'
       });
       boldText = '';
       markAsBold = false;
     } else if (s === 'ITALIC') {
        // same as 'BOLD' process
     } else if (s === 'UNDERLINE') {
        // same as 'BOLD' process
     } else {
        if (markAsBold) {
            boldText += boldText.length > 0 ? s : (' ' + s);
        } else if (markAsItalic) {
            italicText += italicText.length > 0 ? s : (' ' + s);
        } else if (markAsUnderline) {
            underlineText += underlineText.length > 0 ? s : (' ' + s);
        } else {
            textList.push({
                text: s,
                style: 'noStyle'
            })
        }
     }
  }
})
  • Create node based on type in textList.根据 textList 中的类型创建节点。
  textList.forEach(item => {
    let node;
    if (item.style === 'noStyle') {
      // create a span node
    } else if (item.style === 'BOLD') {
      // create a bold node
    } else if (item.style === 'ITALIC') {
      // create a italic node
    } else {
      // create a node and underline it with css.
    }
    // Append to parentNode at last.
  })

暂无
暂无

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

相关问题 需要使用javascript将所选文本设置为粗体/斜体/下划线,或者需要非常基本的文本编辑器建议 - Need to make selected text as bold/italic/underline using javascript, or need very basic text editors suggestions 需要使用javascript将选定的文本设置为粗体/斜体/下划线,并使用c#保存和检​​索相同的文本 - Need to make selected text as bold/italic/underline using javascript, and also save & retrieve the same using c# 我想在 asp mvc 中使用 java 脚本选择 textarea 文本,如粗斜体下划线 - i want to selected textarea text like bold italic underline using java script in asp mvc 如何使用车把呈现斜体/粗体文本? - How to render italic/bold text using handlebars? 使一些文字粗体和一些斜体 - Make some text bold AND some italic 在 Flask 后端存储粗体/斜体文本、图像 - Storing bold/italic text, images in Flask backend 当单击粗体、斜体、下划线、小号时,bootstrap3-wysiwyg 编辑器错误 - bootstrap3-wysiwyg editor error when clicking bold, italic, underline, small 如何改变<p>标记文本,不删除下划线或斜体格式? - How to change <p> tag text, without remove underline or italic format? 如何在javascript中选择粗体/斜体/下划线的文本? - How to make selected text bold/italic/underlined in javascript? 如何将 HTML5 canvas 文本设置为粗体和/或斜体? - How do I style HTML5 canvas text to be bold and/or italic?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM