如何更改数据工具提示消息
how to change data-tooltip message
有没有一种方法可以通过使用javascript控制数据工具提示消息?喜欢html dom吗?
/* Add this attribute to the element that needs a tooltip */
[data-tooltip] {
position: relative;
z-index: 2;
cursor: pointer;
}
/* Hide the tooltip content by default */
[data-tooltip]:before,
[data-tooltip]:after {
visibility: hidden;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
pointer-events: none;
}
/* Position tooltip above the element */
[data-tooltip]:before {
position: absolute;
bottom: 150%;
left: 50%;
margin-bottom: 5px;
margin-left: -80px;
padding: 7px;
width: 160px;
border-radius: 2px;
border: 1px outset #C0C0C0;
box-shadow: 3px 2px 5px #9F9F9F;
background-color: #000;
background-color: hsla(206, 73%, 34%, 0.9);
color: #FFFFFF;
content: attr(data-tooltip);
text-align: center;
font-size: 11px;
font-family: "Trebuchet MS", Helvetica, sans-serif;
font-weight: bold;
line-height: 1.2;
}
/* Triangle hack to make tooltip look like a speech bubble */
[data-tooltip]:after {
position: absolute;
bottom: 150%;
left: 50%;
margin-left: -5px;
width: 0;
border-top: 5px solid #000;
border-top: 5px solid hsla(206, 73%, 34%, 0.9);
border-right: 5px solid transparent;
border-left: 5px solid transparent;
content: " ";
font-size: 0;
line-height: 0;
}
/* Show tooltip content on hover */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
visibility: visible;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
<a href="#" id="abc" data-tooltip="abc"><img src="http://www.w3schools.com/images/w3schools_green.jpg"/ ></a>
当然可以。使用(content: attr(data-tooltip)
)从属性data-tooltip
获取的属性文本。
因此,您只需要将属性的值更改为您想要的值。
function changeTooltip() {
document.getElementById('abc').setAttribute('data-tooltip', 'aaa');
}
/* Add this attribute to the element that needs a tooltip */
[data-tooltip] {
position: relative;
z-index: 2;
cursor: pointer;
}
/* Hide the tooltip content by default */
[data-tooltip]:before,
[data-tooltip]:after {
visibility: hidden;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
pointer-events: none;
}
/* Position tooltip above the element */
[data-tooltip]:before {
position: absolute;
bottom: 150%;
left: 50%;
margin-bottom: 5px;
margin-left: -80px;
padding: 7px;
width: 160px;
border-radius: 2px;
border: 1px outset #C0C0C0;
box-shadow: 3px 2px 5px #9F9F9F;
background-color: #000;
background-color: hsla(206, 73%, 34%, 0.9);
color: #FFFFFF;
content: attr(data-tooltip);
text-align: center;
font-size: 11px;
font-family: "Trebuchet MS", Helvetica, sans-serif;
font-weight: bold;
line-height: 1.2;
}
/* Triangle hack to make tooltip look like a speech bubble */
[data-tooltip]:after {
position: absolute;
bottom: 150%;
left: 50%;
margin-left: -5px;
width: 0;
border-top: 5px solid #000;
border-top: 5px solid hsla(206, 73%, 34%, 0.9);
border-right: 5px solid transparent;
border-left: 5px solid transparent;
content: " ";
font-size: 0;
line-height: 0;
}
/* Show tooltip content on hover */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
visibility: visible;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
<a href="#" id="abc" data-tooltip="abc"><img src="http://www.w3schools.com/images/w3schools_green.jpg"/ ></a>
<br />
<button onclick="changeTooltip()">Change tooltip to "aaa"</button>
如中所述https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes数据属性可以通过dataset
对象访问,使用属性名称在data-
之后的部分。
在您的示例中:
var tlink = document.getElementById('abc');
tlink.dataset.tooltip = "def";
/* Add this attribute to the element that needs a tooltip */
[data-tooltip] {
position: relative;
z-index: 2;
cursor: pointer;
}
/* Hide the tooltip content by default */
[data-tooltip]:before,
[data-tooltip]:after {
visibility: hidden;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
pointer-events: none;
}
/* Position tooltip above the element */
[data-tooltip]:before {
position: absolute;
bottom: 150%;
left: 50%;
margin-bottom: 5px;
margin-left: -80px;
padding: 7px;
width: 160px;
border-radius: 2px;
border: 1px outset #C0C0C0;
box-shadow: 3px 2px 5px #9F9F9F;
background-color: #000;
background-color: hsla(206, 73%, 34%, 0.9);
color: #FFFFFF;
content: attr(data-tooltip);
text-align: center;
font-size: 11px;
font-family: "Trebuchet MS", Helvetica, sans-serif;
font-weight: bold;
line-height: 1.2;
}
/* Triangle hack to make tooltip look like a speech bubble */
[data-tooltip]:after {
position: absolute;
bottom: 150%;
left: 50%;
margin-left: -5px;
width: 0;
border-top: 5px solid #000;
border-top: 5px solid hsla(206, 73%, 34%, 0.9);
border-right: 5px solid transparent;
border-left: 5px solid transparent;
content: " ";
font-size: 0;
line-height: 0;
}
/* Show tooltip content on hover */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
visibility: visible;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
<a href="#" id="abc" data-tooltip="abc"><img src="http://www.w3schools.com/images/w3schools_green.jpg"/ ></a>
var anchor = document.getElementById('abc');
anchor.onmouseover = function(){
this.setAttribute('data-tooltip', 'hello sir');
};
小提琴https://jsfiddle.net/omarCreativeDev/6rpxLu4p/
相关文章:
- d3.js Chord图的动态工具提示
- Graphiti中是否有任何工具提示功能
- "工具提示"jQuery插件坏了
- 单元格的工具提示或标题不显示超过2000个字符
- d3.js用按钮更新条形图工具提示
- 使Intro.js工具提示响应
- 画布中绘制的矩形区域的弹出工具提示
- 使用CSS/JavaScript更改剑道图工具提示文本颜色
- 禁用NVD3 multiBarHorizontalChart中0值的工具提示
- 如何更改已显示的工具提示
- 如何访问高图表工具提示中的任何特定数据
- 如何在jQuery点击函数中添加并显示引导工具提示
- 如何为json键和值提供工具提示消息
- 如何在引导工具提示中显示数据库消息
- 如何更改数据工具提示消息
- 在页面加载时自动对焦 dx文本框 并将 dx工具提示显示为验证消息
- 带有qtip的动态javascript数据覆盖了具有相同消息的所有工具提示
- 从JavaScript动态加载工具提示消息
- 工具提示与错误消息
- 工具提示为不同的元素显示相同的消息