如何仅使用 CSS3 在悬停时更改表格单元格的文本

发布于 2025-01-02 23:52:02 字数 1561 浏览 0 评论 0原文

我想知道 SO 中是否有人愿意帮助我。仅使用 CSS(最有可能是 CSS3),有没有办法在将鼠标悬停在元素上时更改表格单元格的内部 HTML?我有一个包含 49 个单元格(7 行 x 7 列)的数值表,我希望第一行第一个单元格中的数字从数字 1 更改为数字 50,但仅限于将鼠标悬停在数字 1 上时(即 -不悬停时变回数字 1)。

我可以使用 JavaScript 中的“更改innerHTML”函数来执行此操作,但仅限于在文档正文中使用与 HTML 内联的脚本部分。由于各种原因,我不能使用任何脚本或CSS内联,所以这种实现我的目标的方法不是我想要使用的(这超出了语义原因)。我真的宁愿避免使用任何脚本来实现这种效果,因为我认为 CSS3 比 JavaScript 更优雅、更有选择性地处理效果(即 - CSS3 工具提示比任何基于脚本的工具提示要好得多)。

我只是想知道是否有人知道如何使用 CSS3 来做到这一点(也许使用 z-index、display: none; 或某种定位技术?}。我已经玩过它,但我似乎无法弄清楚如果我不必将脚本与我的标记混合在一起,我会使用 JavaScript,但似乎没有办法做到这一点,

谢谢您的宝贵时间。

更新

@ramsesoriginal @hiphip

再次感谢。我对“这个答案对您有帮助吗”的回答是“是”。我相信这就是您所说的“已接受”的意思ramsesoriginal;正确的?也感谢hiphip您的回答。我正在尝试像下面的代码这样的样式,但它在表格单元格中并没有完全按照我希望的方式工作(顺便说一下,与独立图像配合得很好)。我想我会继续努力;选择越多越好。

div.up {
   margin: 10px 0;
   position: relative;
   width: 40px;
   height: 40px;
   border: 1px solid rgb(170, 169, 169);
   overflow: hidden;
}   

div.up div {
   width: 40px;
   height: 40px;
   font-size: 13px;
   padding: 10px;
   position: absolute;
   top: 0;
   left: 0;
   text-align: center;
   background: rgba(255, 255, 255, 1.000);
  -moz-transition: left 1s linear;
  -webkit-transition: left 1s linear;
  -o-transition: left 1s linear;
  transition: left 1s linear;
}

div.up div.one {
   z-index: 999;
}       

div.up:hover div.one {
     -webkit-transition: left 1s linear;
     -moz-transition: left 1s linear;
     -o-transition: left 1s linear;
     transition: left 1s linear;
     left: -99px;
}

I was wondering if anyone in SO would be kind enough to assist me. Using only CSS (most-likely CSS3), is there a way to change the inner HTML of a table cell while hovering over the element? I have a numerical table with 49 cells (7 rows by 7 columns), and I would like the number in the first cell of the first row to change from number 1 to number 50, but only when hovering over the number 1 (i.e. - changing back to number 1 when not hovering).

I can do this with a "change innerHTML" function in JavaScript, but only when using a portion of the script inline with my HTML within the body of the document. For various reasons, I cannot use any script or CSS inline, so this method of achieving my goal is not what I want to use (this goes beyond semantic reasons). I would really rather avoid using any script at all for this effect because I think CSS3 handles effects more elegantly and selectively than JavaScript (i.e. - CSS3 Tooltips are much nicer than any script-based Tooltip).

I was just wondering if someone knew how to do this using CSS3 (maybe with the z-index, display: none; or positioning techniques somehow?}. I've played around with it, but I can't seem to figure it out. I would use JavaScript if I didn't have to mix the script in with my Markup, but there doesn't appear to be a way to do that.

Anyone have ideas on how to go about this? Thank you for your time.

Update

@ramsesoriginal
@hiphip

Thanks again. I answered "Yes" to the "Did this answer help you." I believe that is what you meant by "as accepted" ramsesoriginal; right? Thanks hiphip for your answer as well. I was playing around with styles like the code below, but it wasn't quite working out in the table cell the way I had hoped (works nice with isolated images by the way). I think I'll keep working on it though; the more options, the better.

div.up {
   margin: 10px 0;
   position: relative;
   width: 40px;
   height: 40px;
   border: 1px solid rgb(170, 169, 169);
   overflow: hidden;
}   

div.up div {
   width: 40px;
   height: 40px;
   font-size: 13px;
   padding: 10px;
   position: absolute;
   top: 0;
   left: 0;
   text-align: center;
   background: rgba(255, 255, 255, 1.000);
  -moz-transition: left 1s linear;
  -webkit-transition: left 1s linear;
  -o-transition: left 1s linear;
  transition: left 1s linear;
}

div.up div.one {
   z-index: 999;
}       

div.up:hover div.one {
     -webkit-transition: left 1s linear;
     -moz-transition: left 1s linear;
     -o-transition: left 1s linear;
     transition: left 1s linear;
     left: -99px;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

混吃等死 2025-01-09 23:52:02

有两种方法,但都需要一些额外的标记:

<td id="example1" class="hoverer"><span class="nohower">1</span><span class="hover">50</span></td>

使用样式

 #example1 .hover{
     display:none;
 }

 #example1 .nohower{
     display:block;
 }

 #example1:hover .hover{
     display:block;
 }

 #example1:hover .nohower{
     display:none;
 }

<td id="example2" class="hoverer"><span data-hover="50">1</span></td>

使用样式

 #example2:hover span:after{
     content:attr(data-hover);
 }
 #example2:hover span{
     width:1px;
     margin-left: -0.5em;/* adjust accoridng to font*/
 }

您可以在此处查看工作演示:http://jsfiddle.net/ramsesoriginal/W8LQq/

There are two ways, but both require some additional markup:

<td id="example1" class="hoverer"><span class="nohower">1</span><span class="hover">50</span></td>

with the styling

 #example1 .hover{
     display:none;
 }

 #example1 .nohower{
     display:block;
 }

 #example1:hover .hover{
     display:block;
 }

 #example1:hover .nohower{
     display:none;
 }

or

<td id="example2" class="hoverer"><span data-hover="50">1</span></td>

with the styling

 #example2:hover span:after{
     content:attr(data-hover);
 }
 #example2:hover span{
     width:1px;
     margin-left: -0.5em;/* adjust accoridng to font*/
 }

You can view a working demo here: http://jsfiddle.net/ramsesoriginal/W8LQq/

晨光如昨 2025-01-09 23:52:02

这是不可能的。

CSS 仅用于设置 HTML 样式。它无法访问 HTML 本身的属性。

This is not possible.

CSS is only used for styling HTML. It has no access to the attributes of the HTML itself.

梨涡少年 2025-01-09 23:52:02

仅使用 CSS 无法做到这一点

No way to do this with only CSS

孤蝉 2025-01-09 23:52:02

有定位:

<style>
    #outterdiv {
        position: relative;
        width:20px;
        height:20px;
        overflow:hidden;
        border: 1px blue solid;
    }
    #innerdiv:hover {
        width:20px;
        height:20px;
        position: absolute;
        top: -20px;
    }
</style>

<div id="outterdiv">
    <div id="innerdiv">
        <div>1</div>
        <div>50</div>
    </div>
</div>

With positioning:

<style>
    #outterdiv {
        position: relative;
        width:20px;
        height:20px;
        overflow:hidden;
        border: 1px blue solid;
    }
    #innerdiv:hover {
        width:20px;
        height:20px;
        position: absolute;
        top: -20px;
    }
</style>

<div id="outterdiv">
    <div id="innerdiv">
        <div>1</div>
        <div>50</div>
    </div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文