html - 用 :hover 和 :hover:after改变内容
问题描述
此代码工作正常:
HTML:
<span class="counter" >test</span>
CSS:
.counter:hover:after{
content: "hello";
}
结果:
testhello
但是我怎样才能将“测试”转换为“你好”。我尝试下面的代码,但没有附加任何内容:
CSS:
.counter:hover{
content: "goodbye";
}
预期结果:
goodbye
解决方案
content
只能与::before
和一起使用::after
。content
不是元素内的文本。你应该这样使用它:
.counter:after {
content: "hello";
}
.counter:hover::after {
content: "goodbye";
}
<span class="counter" ></span>
Javascript解决方案:
const counter = document.querySelector(".counter");
counter.addEventListener("mouseover",function(){
this.innerHTML = "hello";
})
counter.addEventListener("mouseout",function(){
this.innerHTML = "goodbye";
})
<span class="counter" >test</span>
推荐阅读
- firebase - 设置文档及其所有子集合的权限
- r - 在重新验证所有输入后如何使用去抖动仅重绘图形
- java - 为什么 Java 的 URLEncoder 不会像 Python 的 urllib.parse.urlencode 那样生成?
- python - Python多线程不是并行执行的
- java - Google Play 已删除我的应用,但我在 Manifest 中找不到违规行?
- r - 为 guide_colorbar 设置默认特征
- vue.js - 在 vuejs 组件中使用刀片 @can 指令
- sql - 将数据类型更改为浮点数并舍入为 2 位小数
- javascript - 使用 jQuery 选择所有至少有 2 个子元素的元素
- php - 如何登录网站并保存 cookie?