简体   繁体   English

使用CSS,JavaScript或jQuery在悬停时更改单词?

[英]Word change on hover using CSS, JavaScript, or jQuery?

I am currently working on a project where I would like a specific word (example: about) within a paragraph to change to a different word (example: abut) when you hover on the original word "about" . 我目前正在一个项目中,当您将鼠标悬停在原始单词“ about”上时,我希望将段落中的特定单词(例如:about)更改为另一个单词(例如:butbut)

The purpose of creating a word change (when hovering over that word) would be for a web visitor to see the original [spelling of a] word from an old document when they hovered over a word, versus just reading the edited version. (将鼠标悬停在该单词上时)创建单词更改的目的是使网络访问者将鼠标悬停在一个单词上时,可以从旧文档中看到单词的原始拼写,而不仅仅是阅读编辑后的版本。

I've played around a little with this in simple CSS (CodePen example here: https://codepen.io/LeahS/pen/LmvKZa ), but would like to know the best way to create a word change as mentioned above. 我已经在简单的CSS(这里的CodePen示例: https ://codepen.io/LeahS/pen/LmvKZa)中进行了一些尝试,但是想知道创建上述单词更改的最佳方法。 Is there a way with just CSS? CSS是否有办法? Or is it better with jQuery or Javascript? 还是使用jQuery或Javascript更好? I currently only know rough JavaScript and jQuery, but will learn both if it helps. 我目前仅了解粗略的JavaScript和jQuery,但如果有帮助的话,我将学习两者。

Appreciate your answers and insights, and thank you for taking time to comment. 感谢您的回答和见解,并感谢您抽出宝贵时间发表评论。

Notes: 笔记:

1: There would be multiple words that must be changed per paragraph, and multiple (more than 100+) paragraphs. 1:每个段落中必须有多个单词必须更改,并且有多个(超过100个以上)段落。 If at all possible, I would NOT like to create unique CSS classes for each word... 如果有可能,我不想为每个单词创建唯一的CSS类。

2: I am/would like to use WordPress. 2:我/想使用WordPress。

3: I am a beginner. 3:我是初学者。

CodePen code: CodePen代码:

<style>
.quote:hover .update, .quote .orig {
  display: none;
}
.quote:hover .orig {
  display: inline-block;
}
.quote .update {
  color: blue;
  font-weight: bold;
}
.quote .orig {
  font-style: italic;
}

</style>

<div>We went
<span class="quote">
<spell class="update">about</spell>
<spell class="orig">abut</spell>
</span>
our
<span class="quote">
<spell class="update">business</spell>
<spell class="orig">busness</spell>
</span>
.
</div>

You could wrap both elements in a span, and drive the hover off of that element, so that the hiding of the children does not affect the hover state. 您可以将两个元素都包裹在一个跨度中,然后将鼠标悬停在该元素上,以使子代的隐藏不会影响悬停状态。

 .swappable .orig { display: none; } .swappable:hover .orig { display: inline-block; } .swappable:hover .update { display: none; } .swappable { color: blue; } 
 <div class="quote"> <div>We went <span class="swappable"> <spell class="update">about</spell> <spell class="orig">abut</spell> </span> our <span class="swappable"> <spell class="update">business</spell> <spell class="orig">busness</spell> </span> . </div> 

I made a couple of changes to the html to make it work, and had both versions of the spelling be within the same span called "change". 我对html进行了一些更改以使其正常工作,并且使两个版本的拼写都在同一范围内,称为“更改”。 It's far from perfect but I suppose it's a start? 这远非完美,但我想这是一个开始吗?

 .changed:hover .update { display: none; } .changed:hover .orig { display: inline-block; } .quote .update { color: blue; font-weight: bold; } .quote .orig { font-style: italic; display:none; } .changed { cursor:pointer } 
 <div class="quote"> <div>We went <span class="changed"><spell class="update">about</spell><spell class="orig">abut</spell></span> our <span class="changed"><spell class="update">business</spell><spell class="orig">busness</spell></span> . </div> 

Showing the other word with plain, vanilla CSS, based on your provided HTML code is as simple as: 根据您提供的HTML代码,使用普通的原始CSS显示另一个单词很简单:

spell.update:hover + spell.orig, spell.orig:hover {
 display:inline-block;
}

If you want to hide the updated spelling when they're looking at the original, you might find it helpful to enclose both in a span, so you can do the following: 如果您想在他们查看原始拼写时隐藏他们的拼写,您可能会发现将两者都用一个跨度括起来很有帮助,因此您可以执行以下操作:

span.spelling-container:hover>.orig {
 display:inline-block;
}
span.spelling-container:hover>.update {
 display:none;
}

You can use data attributes and CSS to display the two different words. 您可以使用数据属性和CSS来显示两个不同的单词。

 span[data-spell] { display: inline-block; border-bottom: 1px dotted gray; cursor: help; } span[data-spell]::before, span[data-spell]::after { display: none; } span[data-spell]::before { content: attr(data-spell); } span[data-spell]::after { content: attr(data-original); } span[data-original]:not(:hover)::before, span[data-spell]:hover::after { display: block; } 
 <div class="quote"> We went <span data-spell="about" data-original="abut"></span> our <span data-spell="business" data-original="busness"></span>. </div> 

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

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