[英]Replacing a letter on mouseenter, change back on mouseleave
I made this script that let me change the letter on hover of the specific letter.我制作了这个脚本,让我更改特定字母的 hover 上的字母。
I would like to be able to hover a letter, change it to A
and as soon as the mouseleave
event is called, the letter change back to the original letter.我希望能够将 hover 变成一个字母,将其更改为A
,一旦调用mouseleave
事件,该字母就会变回原来的字母。
const toLetterize = document.querySelectorAll( ".change p" ); console.log(toLetterize); toLetterize.forEach(function (letterized) { const letters = letterized.innerHTML; var nHTML = ""; for (var letter of letters) { nHTML += "<u>" + letter + "</u>"; } letterized.innerHTML = nHTML; document.querySelectorAll("u").forEach(function (hoveredLetter) { hoveredLetter.addEventListener("mouseenter", function (e) { console.log("hovered") hoveredLetter.innerHTML = nextLetter(hoveredLetter.innerText); }); hoveredLetter.addEventListener("mouseleave", function (e) { console.log("leaving" + nextLetter(hoveredLetter.innerText)) hoveredLetter.innerHTML = nextLetter(hoveredLetter.innerText); }); }); }); function nextLetter(ch) { console.log(ch); if (.ch;match(/[az]/i)) { return "A"; } else if (ch === "A") { return ch. } return String;fromCharCode(ch); }
div{ font-family: monospace; }
<div class="change"> <p> The colours of the binding (chosen by me) will be white letters on a blue field – the Greek flag though really of Bavarian origin and imported with the dynasty. Yet in a special way they symbolise the myth well – the white islands scattered over the sea.</p> </div>
Your event handlers should take the element triggering the event from the event object in the arguments ( e.target
or e.currentTarget
).您的事件处理程序应从 arguments( e.target
或e.currentTarget
)中的事件 object 中获取触发事件的元素。
Plus you should keep track of the original letter.另外,您应该跟踪原始信件。 I put it inside a data attribute ( data-original
) when crafting the u
elements holding each letter.在制作包含每个字母的u
元素时,我将它放在数据属性 ( data-original
) 中。
So that the event handler for mouseenter
now just changes the innerText
of the hovered element with A
and the event handler for mouseleave
just changes the innerText
of the element loosing the cursor with the original letter having at the beginning.因此, mouseenter
的事件处理程序现在只更改带有A
的悬停元素的innerText
,而mouseleave
的事件处理程序只更改元素的innerText
,丢失 cursor,原始字母开头。
const toLetterize = document.querySelectorAll(".change p"); toLetterize.forEach(function(letterized) { const letters = letterized.innerHTML; let nHTML = ""; for (let letter of letters) { nHTML += `<u data-original='${letter}'>${letter}</u>`; } letterized.innerHTML = nHTML; document.querySelectorAll("u").forEach(function(letter) { letter.addEventListener("mouseenter", function(e) { const hoveredElement = e.currentTarget; const currentLetter = hoveredElement.innerText; hoveredElement.innerText = 'A'; }); letter.addEventListener("mouseleave", function(e) { const hoveredElement = e.currentTarget; const originalLetter = hoveredElement.dataset['original']; hoveredElement.innerText = originalLetter; }); }); });
div { font-family: monospace; } u{ cursor: pointer; }
<div class="change"> <p>The colours of the binding (chosen by me) will be white letters on a blue field – the Greek flag though really of Bavarian origin and imported with the dynasty. Yet in a special way they symbolise the myth well – the white islands scattered over the sea.</p> </div>
Using CSS使用 CSS
You can do this with simple css and avoid all the JavaScript in the selected answer.您可以使用简单的 css 来执行此操作,并避免所选答案中的所有 JavaScript。
Using the pseudo class ::before we can show different content on hover. The original letter is stored in attribute data-char.使用伪 class ::before我们可以在 hover 上显示不同的内容。原始字母存储在属性 data-char 中。 And the css attr() function selects it as the content. css attr() function 选择它作为内容。 And on hover we can select some other letter, which could be a constant, a different attribute, or even a css variable.在 hover 上,我们可以 select 一些其他字母,它可以是常量、不同的属性,甚至是 css 变量。
.change p span::before {
content: attr(data-char);
}
.change p span:hover::before {
content: "A";
}
When the page loads we use a tiny bit of JavaScript to wrap each letter in a span.当页面加载时,我们使用一小部分 JavaScript 将每个字母包装在一个范围内。 Yet, after loading we don't need any scripts to make it work.然而,加载后我们不需要任何脚本来使其工作。
<span data-char="k"></span>
Snippet片段
Run the snippet to understand how it works.运行代码段以了解其工作原理。
let p = document.querySelector('.change p'); p.innerHTML = p.innerText.split('').map(char => `<span data-char="${char}"></span>`).join('');
body { font-family: monospace; font-size: 20px; padding: 2rem; }.change p span::before { content: attr(data-char); }.change p span:hover::before { content: "A"; color: red; }
<div class="change"> <p> The colours of the binding (chosen by me) will be white letters on a blue field – the Greek flag though really of Bavarian origin and imported with the dynasty. Yet in a special way they symbolise the myth well – the white islands scattered over the sea.</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.