查询段落中的某个字段 并且高亮显示

在我朋友遇到的前端基础面试题中又遇到过这样的面试题,给你一个input 输入框,button按钮 以及 p段落标签并且在里面有一大段文字,要求如下
在input中输入任何文字,点击button按钮,都需要在p段落中的文段中找到与input输入的文字完全一样的字符并且高亮显示, 逻辑清晰明了,直接上代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        main{
            background-color: gray;
            width:500px;
            margin: 50px auto;
        }
        em{
            color: yellow;
            /* animation-iteration-count: ; */
        }
    </style>
</head>
<body>
    <main>
        <input type="text" id="text1">
    <button id="button1" onclick="search()">查询</button>
    <p id="box">The grounds were covered with dark green grass through which straight white stine path .Oh both sides of the path were planted what I believed to be poinsetting. Now the flowers were surrounded by pinkish leaves instead of bright red ones as they should have been.Trimmed with light light green edges,the leaves looked delivcate and charming.Along the long path I often took a stroll which would take me to a wide terrace. where 1 could take me to a wide terrace. where 1 could watch the enchanting glow of sunset and occasionally catch the sight of a train pulling and hooting on ites way south ward.</p>
    </main>
    <script>
        var text1 = document.getElementById("text1");
        var button1 = document.getElementById("button1");
        var textBox = document.getElementById("box");
        let text = textBox.innerHTML;
        function search(){
            if(!text1.value){
                alert("不能为空")
            }
            textBox.innerHTML = text;
            let newHTML = textBox.innerHTML.split(text1.value).join(`<em>${text1.value}</em>`);
            textBox.innerHTML = newHTML;
        }
        var a = [1,2,3]
        var b = a;
        a.push(4);
        a.push(5);
        console.log(a,b)
        
        console.log(((x,y)=>x+y)(1,2))
        console.log(typeof new Number(1))
        console.log("asd".startsWith("a"))
        "asd".slice
    </script>
</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值