简体   繁体   English

使用伪类选择器更改内容?

[英]Using pseudo class selectors to change content?

How can I change the text which is contained in <p> tags by using CSS's pseudo class selectors? 如何使用CSS的伪类选择器更改<p>标记中包含的文本?

For example, when I hover over a paragraph, the content of paragraph must change to what would be specified in p:hover selector. 例如,当我将鼠标悬停在一个段落上时,该段落的内容必须更改为p:hover选择器中指定的内容。

jsFiddle. jsFiddle。


One way is to use p:hover:before along with the content attribute. 一种方法是将p:hover:beforecontent属性一起使用。

Here's an example: 这是一个例子:

Html: HTML:

<p>
<span>First text!</span>
</p>

CSS: CSS:

p:hover span {
  display:none
}
p:hover:before {
  content:"Second text appears instead!";
  color:red;
}

If you'd like to know more about the content property, check out this nice little article . 如果您想了解更多关于content属性的信息,请查看这篇不错的小文章

Zenith has a simpler solution, but the following allows you to put formatting in your "hover" content. Zenith有一个更简单的解决方案,但是以下内容使您可以将格式设置放在“悬停”内容中。 Try the following HTML: 尝试以下HTML:

<p>
  <span class="normalDisplay">Text to display <em>usually</em>.</span>
  <span class="hoverDisplay">Text to display on <em>hover</em>.</span>
</p>

and the following CSS: 和以下CSS:

p .hoverDisplay {
    display: none;
}
p .normalDisplay {
    display: inline;
}
p:hover .hoverDisplay {
    display: inline;
}
p:hover .normalDisplay {
    display: none;
}

Fiddle 小提琴

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

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