简体   繁体   English

用黄色背景突出显示所有超过 8 个字符的单词。 如何在不使用 jQuery 的情况下仅使用 JavaScript 来解决这个问题?

[英]Highlight all the words over 8 character long with a yellow background. How to do this question with only JavaScript without using jQuery?

I have started learning JavaScript through practice exercises.我已经通过练习练习开始学习JavaScript。 I tried solving this question in the following way but it is not working.. Any leads would be appreciated.我尝试通过以下方式解决这个问题,但它不起作用..任何线索将不胜感激。

 window.onload = function() { check = (word) => { if (word.length > 8) { word.style.color = "blue"; } else { word; } } func = () => { var str = document.querySelector("#Second").innerText; var newt = str.trim().split(' ').map(check).join(' '); document.querySelector("#Second").innerText = newt; } }

I think you problem lies in the check() function.我认为你的问题在于check() function。 You have analyzed the problem correctly, but you don't understand about DOM so you have done it wrong way.您已经正确分析了问题,但是您对 DOM 不了解,因此您做错了。

First, the word you check is pure string (which is an array of char, so that you could check it with length property).首先,您检查的单词是纯字符串(它是一个字符数组,因此您可以使用length属性检查它)。 Second, the .style.color is child object of Element DOM object only.其次, .style.color仅是 Element DOM object 的子 object。 String cannot do that.字符串无法做到这一点。

Because of the problem, you have to convert the string you just check to Element DOM object.由于这个问题,您必须将刚刚检查的字符串转换为 Element DOM object。 Depending on cases, there is many way to do so.根据情况,有很多方法可以做到这一点。 I will assume that the output will be like this:我将假设 output 将是这样的:

document.body.innerHTML += word

If that is the case, you can be at ease.如果是这样,你就可以放心了。 Just simply wrap the word in this html code string.只需简单地将word包装在此 html 代码字符串中即可。 The rest you have solved it well. rest 你已经很好解决了。

(I know you use innerText, but I think innerHTML is much easier so I choose it. If you really need to use innerText, comment below and I will update the post) (我知道你用的是innerText,但我觉得innerHTML更容易所以我选择它。如果你真的需要使用innerText,请在下面评论,我会更新帖子)

Here is my code:这是我的代码:

 window.onload = function() { const check = word => { if (word.length > 8) { return '<span class="hightlight">' + word + '</span>' } else { return word } } const sample = document.querySelector("#sample") sample.innerHTML = sample.innerText.trim().split(' ').map(check).join(' ') }
 #sample {}.hightlight { background: yellow }
 <p id='sample'>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>

My advice.我的建议。 Before doing anything, try to understand the variables' type you are dealing with.在做任何事情之前,请尝试了解您正在处理的变量的类型。

 window.onload = function() { check = (word) => { if (word.length > 8) { word = '<span style="background:yellow;">' + word + '</span>'; } else { word; } return word; } var str = document.querySelector("#Second").innerText; var newt = str.trim().split(' ').map(check).join(' '); document.querySelector("#Second").innerHTML = newt; }
 <div id="Second">Short short thiswordislong short short thiswordislongtoo short</div>

Version with.|,|?带有.|,|? 的版本detection检测

 const setup = () => { const p = document.querySelector('p'); wrapWordsWithLengthEight(p); } const check = (word) => { //Check if word include a.|,|? const hasCharacter = word.includes(".", word.length - 1) || word.includes(",", word.length - 1) || word.includes('?', word.length - 1); //Calculate real word length const wordLength = hasCharacter? (word.length -1): word.length; if(wordLength > 8) { const spanContent = hasCharacter? word.substring(0, word.length - 1): word; const endCharacter = hasCharacter? (word.substring(word.length - 1)): ''; word = `<mark>${spanContent}</mark>${endCharacter} `; } else word = `${word} `; return word; }; const wrapWordsWithLengthEight = (target) => { //Render HTML to target const text = (target.textContent).trim().split(' ').map(check).join(''); target.innerHTML = text; } window.addEventListener('load', setup);
 <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique enim quod quos, modi architecto, praesentium tenetur suscipit assumenda, sit neque odit, illum minima voluptates? Dolor non dolore accusamus quam maiores. </p>

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

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