首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使我的网站更有效率?

如何使我的网站更有效率?
EN

Stack Overflow用户
提问于 2021-01-24 02:00:42
回答 2查看 80关注 0票数 1

我有一个JavaScript文件在我的网站上,采取每一个字,并突出他们各自。有没有办法把它变成一条线而不是所有的单行呢?

JavaScript:

代码语言:javascript
运行
复制
var input = document.getElementById("boch");
input.addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("bocho").click();
}
});

var element = document.querySelector("#boch");

var start;
element.onkeyup = function () {
var value = element.value;

if (value === "m") {
start = Date.now();
}


if (value.includes("man")) {
document.getElementById('word-1').style.backgroundColor = 'green';
} else {
document.getElementById('word-1').style.backgroundColor = "red";
}

if (value.includes("man become")) {
document.getElementById('word-2').style.backgroundColor = 'green';
} else {
document.getElementById('word-2').style.backgroundColor = "red";
}

if (value.includes("man become as")) {
document.getElementById('word-3').style.backgroundColor = 'green';
} else {
document.getElementById('word-3').style.backgroundColor = "red";
}

if (value.includes("man become as and")) {
document.getElementById('word-4').style.backgroundColor = 'green';
} else {
document.getElementById('word-4').style.backgroundColor = "red";
}

if (value.includes("man become as and through")) {
document.getElementById('word-5').style.backgroundColor = 'green';
} else {
document.getElementById('word-5').style.backgroundColor = "red";
}

if (value.includes("man become as and through find")) {
document.getElementById('word-6').style.backgroundColor = 'green';
} else {
document.getElementById('word-6').style.backgroundColor = "red";
}

if (value.includes("man become as and through find would")) {
document.getElementById('word-7').style.backgroundColor = 'green';
} else {
document.getElementById('word-7').style.backgroundColor = "red";
}

if (value.includes("man become as and through find would here")) {
document.getElementById('word-8').style.backgroundColor = 'green';
} else {
document.getElementById('word-8').style.backgroundColor = "red";
}

if (value.includes("man become as and through find would here and")) {
document.getElementById('word-9').style.backgroundColor = 'green';
} else {
document.getElementById('word-9').style.backgroundColor = "red";
}

if (value.includes("man become as and through find would here and before")) {
document.getElementById('word-10').style.backgroundColor = 'green';
} else {
document.getElementById('word-10').style.backgroundColor = "red";
}
var end;
if (value === "man become as and through find would here and before") {
end = Date.now();
}
let millis = end - start;
if (isNaN(millis)) {
  return " ";
}
console.log(millis)

var seconds = millis / 1000;
var min = seconds / 60;

var wpm1 = ((52/5)/min)
console.log(wpm1)
var wpm = Math.round(wpm1);
console.log(wpm)
document.getElementById("bd").innerHTML = wpm;
}

HTML:

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
</head>
<h1>
   <span id="word-1">man</span> <span id="word-2">become</span> <span id="word-3">as</span>
   <span id="word-4">and</span> <span id="word-5">through</span> <span id="word-6">find</span> <span id="word-7">would</span> <span id="word-8">here</span> <span id="word-9">and</span> <span id="word-10">before</span>
</h1>

<input type="text" id="boch" autocomplete="off" onselectstart="return false" onpaste="return false;" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false">

        </div>
        <div id="typing-area">

      <button id="bocho" onclick="document.getElementById('boch').value = ''">Enter</button>

</html>

<h1 id="bd">WPM</h1>



<script src="main.js"></script>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-24 18:34:05

对于这个解决方案,我只使用两个跨度,一个用于绿色部分,一个用于红色文本。然后,我循环遍历通过使用输入的文本,找出文本出错的点。然后,我将答案中的所有文本移到绿色跨度中,其余的将被保留在红色跨度中(这就是切片所做的)。下面是一个工作示例:

HTML:

代码语言:javascript
运行
复制
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>test</title>
</head>

<h1></h1><span id="span1" style="background-color: green;"></span><span id="span2" style="background-color: red;">man become as and through find would here and before</span></h1>
<br>
<input type="text" id="boch" >

<script src="js.js"></script>
</html>

JavaScript

代码语言:javascript
运行
复制
var element = document.querySelector("#boch");

var span1 = document.getElementById("span1");
var span2 = document.getElementById("span2");

element.onkeyup = function () {
    var value = element.value;

    var text = span1.innerText + span2.innerText;

    if (value === text) {
        span2.innerText = "";
        span1.innerText = text;
    } else {
        for (var i = 0; i < value.length; i++) {
            if (text.charAt(i) !== value.charAt(i)) {
                break;
            }
        }

        span1.innerText = text.slice(0, i);
        span2.innerText = text.slice(i, text.length);
    }
};

或者你可以看到它在这里起作用:https://jsfiddle.net/8des04y5/

票数 0
EN

Stack Overflow用户

发布于 2021-01-24 13:55:21

我想说的是,第一个技巧是在变量中存储您正在多个地方使用的代码块。这是枯燥的原则,不要重复。例如:

代码语言:javascript
运行
复制
if (value.includes("man")) {
document.getElementById('word-1').style.backgroundColor = 'green';
} else {
document.getElementById('word-1').style.backgroundColor = "red";
}

并使用此变量替换相同的代码:

const word1 = document.getElementById('word-1');

代码语言:javascript
运行
复制
 if (value.includes("man")) {
    word1.style.backgroundColor = 'green';
    } else {
    word1.style.backgroundColor = "red";
    }

对您将多次使用的每一段代码都执行此操作。当然,这只是一个例子,您可以做得更好,因为您有很多重复的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65866483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档