简体   繁体   English

尝试更改特定字母的背景颜色

[英]trying to change background colour for specific letters

I want the javascript code to highlight the background colour for all 'a' and 'i' letters in the following two paragraph tags. 我希望javascript代码在以下两个段落标签中突出显示所有'a'和'i'字母的背景颜色。

I also want the image tag to show correctly. 我也希望图像标签能够正确显示。 I wasn't linking to an image file in this test so showing correctly just means showing the default image. 我没有在此测试中链接到图像文件,因此正确显示只是意味着显示默认图像。

At one point i had this code working as expected but then when i tried it later it was no longer working. 在某一时刻,我的代码按预期工作,但是后来我尝试时,它不再工作。 The only conclusion is i must have changed something but i don't know what. 唯一的结论是我一定已经改变了一些东西,但是我不知道是什么。

Can anyone tell me why it doesn't work? 谁能告诉我为什么它不起作用?

 document.querySelectorAll('p').forEach(function(myFirstItem, index) { container = '' zeroORone = 0 myFirstItem.innerHTML.split('').forEach(function(mySecondItem) { if (mySecondItem == 'a' & zeroORone == 0) { container += '<span class="yellow">' + mySecondItem + '</span>' } else if (mySecondItem == 'i' & zeroORone == 0) { container += '<span class="orange">' + mySecondItem + '</span>' } else if (mySecondItem == '<') { zeroORone = 1 container += mySecondItem } else if (mySecondItem == '>') { zeroORone = 0 container += mySecondItem } else { container += mySecondItem } }) myFirstItem.innerHTML = container }) 
 <p>piece of a puzzle</p> <p>Planet <img src="someImage.png"> Earth</p> 

I've made some changes to the HTML: I've wrapped the text in span elements. 我对HTML进行了一些更改:我将文本包装在span元素中。 In the javascript I'm searching for the a and the i and I'm wrapping them in a <i> element. 在javascript中,我正在搜索ai ,并将它们包装在<i>元素中。 Please take a look: 请看一下:

 let spans = document.querySelectorAll('span') spans.forEach(s=>{ let text = s.innerHTML; text = text.replace(/([ai])/g, (found)=> { return `<i class='${found}'>${found}</i>` }); s.innerHTML = text; }) 
 ia{background:yellow} ii{background:orange} 
 <p><span>piece of a puzzle</span></p> <p><span>Planet</span> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/pin.png"> <span>Earth</span></p> 

Your code does work, except you have not added css for classes yellow and orange . 您的代码可以正常工作,只不过您没有为类yelloworange添加css

 document.querySelectorAll('p').forEach(function(myFirstItem, index) { container = '' zeroORone = 0 myFirstItem.innerHTML.split('').forEach(function(mySecondItem) { if (mySecondItem == 'a' && zeroORone == 0) { container += '<span class="yellow">' + mySecondItem + '</span>' } else if (mySecondItem == 'i' && zeroORone == 0) { container += '<span class="orange">' + mySecondItem + '</span>' } else if (mySecondItem == '<') { zeroORone = 1 container += mySecondItem } else if (mySecondItem == '>') { zeroORone = 0 container += mySecondItem } else { container += mySecondItem } }); myFirstItem.innerHTML = container }); 
 .yellow { background: yellow; } .orange { background: orange; } 
 <p>piece of a puzzle</p> <p>Planet <img src="someImage.png"> Earth</p> 

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

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