我有一个应用程序,只能更改CSS样式。
例如:<p class="abc">My Text</p>
我只能更改上述文本中的abc,或者可以在abc旁添加另一个css类。 有没有办法通过编辑css中abc的属性来显示工具提示? 我只能编辑css文件。 即使工具提示文本也必须放置在css中,或者css可以从任何其他地方引用它。 有任何可能性吗?
可以用纯CSS完成吗?可以。但是,使用JavaScript可以得到更多的控制。
body { margin: 3em; }
.tooltip { position: relative; }
.tooltip::before {
content: "\2003" attr(class); /* print em-space with class text */
text-indent: -3.9em; /* add negative text offset to hide class name */
display: inline-block;
position: absolute; bottom: 50%;
background: #000; color: #FFF; padding: 5px; border-radius: 5px;
opacity:0; transition:0.3s; overflow:hidden;
max-width: 50%; /* avoids very long sentences */
pointer-events: none; /* prevents tooltip from firing on pseudo hover */
}
.tooltip:hover::before { opacity:1; bottom: 100%; }
<p class="tooltip Tooltip Text">My Text</p>
<p class="tooltip Dolor sit amet bla bla">Lorem Ipsum</p>
text-indent
和overflow:hidden
来隐藏文本的前部分,使得tooltip My Text here
变成了My Text here
。这有点hacky,你需要注意类名冲突。max-width
属性 - https://jsfiddle.net/azizn/gs1ye10r/2/ 我还添加了 pointer-events: none
,这样当工具提示被意外触发时就不会有反应。 - Aziz:not
伪选择器,比如.hello:not(.tooltip)
。 - Aziz.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
bottom: 100%;
left: 50%;
margin-left: -60px;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
<p>Move the mouse over the text below:</p>
<div class="tooltip">Hover over me
<span class="tooltiptext">Tooltip text</span>
</div>
abc" title="tooltip
。顺便说一下,如果这样做成功了,应用程序就没有受到XSS攻击的保护。 - Mr Lister