简体   繁体   English

如何使用javascript从句子中选择单词?

[英]How to select word from sentence using javascript?

Suppose I a complete sentence when I click on any of the words I was trying to highlight that word and read the value into a javascript variable.假设当我点击任何我试图突出显示该词并将该值读入 javascript 变量的词时,我是一个完整的句子。

If I click on the three words then all three words should be highlighted by green color and store the value as a string in a variable.如果我点击这三个词,那么所有三个词都应该以绿色突出显示,并将值作为字符串存储在变量中。

<p onclick="handleClicks()" class="word" >
  Select the choice words that make up the complete sentence.
</p>

Now if I click on Select , words and complete then it should be highlighted and all the selected values should be saved inside a variable in javascript.现在,如果我单击Selectwordscomplete那么它应该突出显示,并且所有选定的值都应该保存在 javascript 中的变量中。

Is it possible to do so using javascript?是否可以使用 javascript 这样做?

What I tried is我试过的是

const handlClicks = (event) => {
  const clickedElem = event.target
  clickedElem.classList.toggle('active');
}

But this does not work.但这不起作用。 should I use multiple span tag inside of the p tag and then apply a function to all the span我应该在 p 标签内使用多个 span 标签,然后将函数应用于所有 span

You can add the words dynamically, as they're wrapped within span elements.您可以动态添加单词,因为它们包含在span元素中。

Then add the logic by iterating them.然后通过迭代添加逻辑。 For example:例如:

 const sentence = 'Select the choice words that make up the complete sentence.'; const words = sentence.split(' '); document.getElementById('content').innerHTML = words.reduce((acc, word) => { return `${acc} <span class="word">${word}</span>`; }, ''); const elements = document.getElementsByClassName('word'); [...elements].forEach((element) => { element.addEventListener('click', () => { element.classList.toggle('selected'); }) });
 .word { cursor: pointer; } .selected { background-color: palegreen; }
 <div id="content"></div>

If you want select word's near search input, you can try this.如果你想选择单词的近搜索输入,你可以试试这个。

 function func(){ _func(); var search = document.getElementById('search'); var demo = document.getElementById('demo'); var bring = demo.innerHTML; var bloototh = bring.indexOf(search.value); if (bloototh >= 0) { bring = bring.substring(0,bloototh) + "<span id='index' class='style'>" + bring.substring(bloototh,bloototh+search.value.length) + "</span>" + bring.substring(bloototh + search.value.length); demo.innerHTML = bring; } } function _func(){ var index = document.getElementById('index'); if(index !== null){ index.outerHTML = index.innerHTML; } }
 input{ padding: 10px; margin: 10px; } .style { color: white; background: gray; }
 <input type="text" id="search"><br> <input onclick="func();" value="Show" type="button"> <p id="demo">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM