javascript 如何在可滚动的文本区域中查找当前显示的文本

eufgjt7s  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(107)

我有一个网页,有一个大的可滚动的textarea元素,其中包含几页的文本。如何使用JavaScript识别当前显示的文本行?
我可以使用scrollTop来找出用户在文本区域中滚动了多远,但这并不允许我准确地识别显示的文本,因为顶部的文本经常有换行,而底部的文本没有换行(因此,如果scrollTop是scrollHeight的50%,则文本不到文本行的50%)。
背景:屏幕左侧的文本区域包含标记语言的文本。屏幕右侧的显示区域将数据显示为html格式的文本。当我在左边的textarea中滚动标记的文本时,我希望html格式的显示区域同步滚动。然而,由于格式的差异,我不能只使用scrollTop和scrollHeight。
有什么建议吗?我正在寻找一个HTML,JavaScript和/或CSS的解决方案。谢谢

ru9i0ody

ru9i0ody1#

const quizData = [{
        question: "Which of the following is a client site language?",
        a: "Java",
        b: "C",
        c: "Python",
        d: "JavaScript",
        correct: "d",
    },
    {
        question: "What does HTML stand for?",
        a: "Hypertext Markup Language",
        b: "Cascading Style Sheet",
        c: "Jason Object Notation",
        d: "Helicopters Terminals Motorboats Lamborginis",
        correct: "a",
    },
    {
        question: "What year was JavaScript launched?",
        a: "1996",
        b: "1995",
        c: "1994",
        d: "none of the above",
        correct: "b",
    },
    {
        question: "What does CSS stands for?",
        a: "Hypertext Markup Language",
        b: "Cascading Style Sheet",
        c: "Jason Object Notation",
        d: "Helicopters Terminals Motorboats Lamborginis",
        correct: "b",
    }
];
let index = 0;
let correct = 0,
    incorrect = 0,
    total = quizData.length;
let questionBox = document.getElementById("questionBox");
let allInputs = document.querySelectorAll("input[type='radio']")
const loadQuestion = () => {
    if (total === index) {
        return quizEnd()
    }
    reset()
    const data = quizData[index]
    questionBox.innerHTML = `${index + 1}) ${data.question}`
    allInputs[0].nextElementSibling.innerText = data.a
    allInputs[1].nextElementSibling.innerText = data.b
    allInputs[2].nextElementSibling.innerText = data.c
    allInputs[3].nextElementSibling.innerText = data.d
}

document.querySelector("#submit").addEventListener(
    "click",
    function() {
        const data = quizData[index]
        const ans = getAnswer()
        if (ans === data.correct) {
            correct++;
        } else {
            incorrect++;
        }
        index++;
        loadQuestion()
    }
)

const getAnswer = () => {
    let ans;
    allInputs.forEach(
        (inputEl) => {
            if (inputEl.checked) {
                ans = inputEl.value;
            }
        }
    )
    return ans;
}

const reset = () => {
    allInputs.forEach(
        (inputEl) => {
            inputEl.checked = false;
        }
    )
}

const quizEnd = () => {
    // console.log(document.getElementsByClassName("container"));
    document.getElementsByClassName("container")[0].innerHTML = `
        <div class="col">
            <h3 class="w-100"> Hii, you've scored ${correct} / ${total} </h3>
        </div>
    `
}
loadQuestion(index);
xytpbqjk

xytpbqjk2#

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/custom.css" />
</head>

<body>
    <section class="main">

        <div class="container">
            <div class="col">
                <h3 id="questionBox">
                    1) Lorem ipsum dolor sit amet, consectetur adipisicing elit Debitis?
                </h3>
            </div>
            <div class="col box">
                <input name="option" type="radio" id="first" value="a" required>
                <label for="first">Testing 1</label>
            </div>
            <div class="col box">
                <input name="option" type="radio" id="second" value="b" required>
                <label for="second">Testing 2</label>
            </div>
            <div class="col box">
                <input name="option" type="radio" id="third" value="c" required>
                <label for="third">Testing 3</label>
            </div>
            <div class="col box">
                <input name="option" type="radio" id="fourth" value="d" required>
                <label for="fourth">Testing 4</label>
            </div>
            <button id="submit">Submit</button>
        </div>

    </section>
    <script src="js/app.js"></script>
</body>

</html>

相关问题