【问题标题】:color background words in the text?文本中的颜色背景词?
【发布时间】:2021-03-24 06:51:43
【问题描述】:

我有这个代码,但我有一个单词列表,例如:

 liste1= ["Robotics","machines","engineering"]
 liste2= ["and","that","of"] 

我想为文本中的单词背景着色:

if word in the text is in the liste1 , color the background with "red" color
if word in the text is in the liste2 , color the background with "green" color

我的代码 html:

<div id="highlights">
        <div class="container">
            <div class="row">
                <div class="col-md-12" id="paragraph">
                    <p>
                        Robotics is an interdisciplinary branch of engineering and science that includes mechanical engineering, electronics engineering, information engineering, computer science, and others. Robotics deals with the design, construction, operation, and use of robots, as well as computer systems for their control, sensory feedback, and information processing.
                        These technologies are used to develop machines that can substitute for humans and replicate human actions. Robots can be used in many situations and for lots of purposes, but today many are used in dangerous environments (including bomb detection and deactivation), manufacturing processes, or where humans cannot survive (e.g. in space). Robots can take on any form but some are made to resemble humans in appearance. This is said to help in the acceptance of a robot in certain replicative behaviors usually performed by people. Such robots attempt to replicate walking, lifting, speech, cognition, and basically anything a human can do. Many of today's robots are inspired by nature, contributing to the field of bio-inspired robotics.
                        The concept of creating machines that can operate autonomously dates back to classical times, but research into the functionality and potential uses of robots did not grow substantially until the 20th century.[1] Throughout history, it has been frequently assumed that robots will one day be able to mimic human behavior and manage tasks in a human-like fashion. Today, robotics is a rapidly growing field, as technological advances continue; researching, designing, and building new robots serve various practical purposes, whether domestically, commercially, or militarily. Many robots are built to do jobs that are hazardous to people such as defusing bombs, finding survivors in unstable ruins, and exploring mines and shipwrecks. Robotics is also used in STEM (science, technology, engineering, and mathematics) as a teaching aid.
                        Robotics is a branch of engineering that involves the conception, design, manufacture, and operation of robots. This field overlaps with electronics, computer science, artificial intelligence, mechatronics, nanotechnology and bioengineering.
                    </p>
                </div>
                <div class="col-md-12 input-group mt-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">
                            <i class="fas fa-pencil-alt"></i>
                        </span>
                    </div>
                   
                </div>
            </div>
        </div>
    </div>

我想根据列表中的单词输出这段代码,红色表示liste1,绿色表示liste2,非常感谢

【问题讨论】:

    标签: javascript html web


    【解决方案1】:

    然后你只需要查找它在哪个列表中。

    paragraph.innerHTML = paragraph.textContent.replace(regexp, () => {
        let col = liste1.includes(this.value) ? 'red' : 'green';
        return '<span style="color:'+col+'">' + this.value + '</span>'
    });
    

    (你没有说如果输入的值在两个列表中都找不到会发生什么。)

    【讨论】:

    • 对不起!如果当前单词在 liste1 中,我只想将文本中的单词着色为红色,如果当前单词在 liste2 中,我想将文本中的单词着色为绿色,我更新了我的帖子以使其更清晰,所以,将文本中的单词着色为红色如果 liste1 中的文本中的当前单词 绿色如果 liste2 中的文本中的当前单词 else,什么都不做
    【解决方案2】:

    我使用indexOf() 添加了两个if 条件来访问数据数组。另外,你需要在逻辑的开头声明一个数组:

    var liste1 = ["Robotics","machines","engineering"];
    var liste2 = ["and","that","of"];
    

    我是否正确理解了您的问题?

    var input = document.getElementById('typed-text');
    var liste1 = ["Robotics","machines","engineering"];
    var liste2 = ["and","that","of"];
    
    input.onkeydown = function (e) {
    
        if (e.keyCode === 13) {
    
            var paragraph = document.getElementById('paragraph');
            var result = document.querySelector('.result-output');
            var regexp = new RegExp(this.value, 'g');
            var textIncludes = paragraph.textContent.match(regexp);
                
            if (result)
                result.remove();
          
      if (liste1.indexOf(this.value) >= 0) {
        paragraph.innerHTML = paragraph.textContent.replace(regexp,
        '<span style="color:white; background: red;">' + this.value + '</span>');
      } else if (liste2.indexOf(this.value) >= 0) {
        paragraph.innerHTML = paragraph.textContent.replace(regexp,
        '<span style="color:white; background: green;">' + this.value + '</span>');
      } else 
      {      
        paragraph.innerHTML = paragraph.textContent.replace(regexp,
        '<span style="color:red">' + this.value + '</span>');
      }
            
    
            paragraph.insertAdjacentHTML(
                'afterend',
                '<span class="result-output" style="display: block; padding: 5px; margin-top: 10px; background: #eee; color: green;">' + (textIncludes ? textIncludes.length : 0) + ' words has been found.</span>');
                
        }
    
    }
    <div id="highlights">
            <div class="container">
                <div class="row">
                    <div class="col-md-12" id="paragraph">
                        <p>
                            Robotics is an interdisciplinary branch of engineering and science that includes mechanical engineering, electronics engineering, information engineering, computer science, and others. Robotics deals with the design, construction, operation, and use of robots, as well as computer systems for their control, sensory feedback, and information processing.
                            These technologies are used to develop machines that can substitute for humans and replicate human actions. Robots can be used in many situations and for lots of purposes, but today many are used in dangerous environments (including bomb detection and deactivation), manufacturing processes, or where humans cannot survive (e.g. in space). Robots can take on any form but some are made to resemble humans in appearance. This is said to help in the acceptance of a robot in certain replicative behaviors usually performed by people. Such robots attempt to replicate walking, lifting, speech, cognition, and basically anything a human can do. Many of today's robots are inspired by nature, contributing to the field of bio-inspired robotics.
                            The concept of creating machines that can operate autonomously dates back to classical times, but research into the functionality and potential uses of robots did not grow substantially until the 20th century.[1] Throughout history, it has been frequently assumed that robots will one day be able to mimic human behavior and manage tasks in a human-like fashion. Today, robotics is a rapidly growing field, as technological advances continue; researching, designing, and building new robots serve various practical purposes, whether domestically, commercially, or militarily. Many robots are built to do jobs that are hazardous to people such as defusing bombs, finding survivors in unstable ruins, and exploring mines and shipwrecks. Robotics is also used in STEM (science, technology, engineering, and mathematics) as a teaching aid.
                            Robotics is a branch of engineering that involves the conception, design, manufacture, and operation of robots. This field overlaps with electronics, computer science, artificial intelligence, mechatronics, nanotechnology and bioengineering.
                        </p>
                    </div>
                    <div class="col-md-12 input-group mt-3">
                        <div class="input-group-prepend">
                            <span class="input-group-text" id="basic-addon1">
                                <i class="fas fa-pencil-alt"></i>
                            </span>
                        </div>
                        <input id="typed-text" type="text" class="form-control" placeholder="Type text">
                    </div>
                </div>
            </div>
        </div>

    【讨论】:

    • 对不起,您所做的是:如果输入单词在文本中,则在文本中用红色将其着色,但我,我只想显示不同颜色的文本,而不是依赖于输入单词,我只想根据单词显示不同颜色的文本,如果liste1中的单词颜色=红色,否则颜色为绿色,在输出中,我希望在liste1和绿色的单词处显示一个红色的文本在 liste2 中的单词
    • 做你已经做的事,但自动完成,不依赖于输入的单词
    • @MohamedOuerfelli,但在您写的问题中,您需要为文本的背景着色,而不是文本。
    • 是的背景,对不起,是的,我想做你正在做的事情,但自动,不依赖于输入的单词
    【解决方案3】:

    这是另一种看法。我在一个常见的colors 对象中组织了颜色和图案。然后由Object.entries() 上的嵌套循环处理,然后在颜色数组的元素上处理:

    const colors={red:["Robotics","machines","engineering"],
                  blue:["and","that","of"]};
    const p=document.querySelector("#paragraph p");
    var txt=p.textContent;
    
    Object.entries(colors).forEach(([col,pats])=>
      pats.forEach(pat=>
        txt=txt.replace(new RegExp(pat,'gi'),'<span class="'+col+'">$&</span>') ) );
    p.innerHTML=txt;
    .red {background-color:pink}
    .blue {background-color:lightblue}
    <div id="highlights">
            <div class="container">
                <div class="row">
                    <div class="col-md-12" id="paragraph">
                        <p>
                            Robotics is an interdisciplinary branch of engineering and science that includes mechanical engineering, electronics engineering, information engineering, computer science, and others. Robotics deals with the design, construction, operation, and use of robots, as well as computer systems for their control, sensory feedback, and information processing.
                            These technologies are used to develop machines that can substitute for humans and replicate human actions. Robots can be used in many situations and for lots of purposes, but today many are used in dangerous environments (including bomb detection and deactivation), manufacturing processes, or where humans cannot survive (e.g. in space). Robots can take on any form but some are made to resemble humans in appearance. This is said to help in the acceptance of a robot in certain replicative behaviors usually performed by people. Such robots attempt to replicate walking, lifting, speech, cognition, and basically anything a human can do. Many of today's robots are inspired by nature, contributing to the field of bio-inspired robotics.
                            The concept of creating machines that can operate autonomously dates back to classical times, but research into the functionality and potential uses of robots did not grow substantially until the 20th century.[1] Throughout history, it has been frequently assumed that robots will one day be able to mimic human behavior and manage tasks in a human-like fashion. Today, robotics is a rapidly growing field, as technological advances continue; researching, designing, and building new robots serve various practical purposes, whether domestically, commercially, or militarily. Many robots are built to do jobs that are hazardous to people such as defusing bombs, finding survivors in unstable ruins, and exploring mines and shipwrecks. Robotics is also used in STEM (science, technology, engineering, and mathematics) as a teaching aid.
                            Robotics is a branch of engineering that involves the conception, design, manufacture, and operation of robots. This field overlaps with electronics, computer science, artificial intelligence, mechatronics, nanotechnology and bioengineering.
                        </p>
                    </div>
                </div>
            </div>
        </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-09
      • 2019-04-22
      • 2023-04-10
      相关资源
      最近更新 更多