如何更改data-tooltip消息

时间:2016-03-28 06:47:38

标签: javascript html css tooltip

有没有办法可以使用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>  
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

当然可以。使用(<html> <head> <link rel="stylesheet" href="style.css"> <script data-require="jquery" data-semver="2.2.0" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <script src="script.js"></script> </head> <body> <h1>Hello Plunker!</h1> <a class="clickToStyle">change Style</a> </body> </html> )从属性$(document).on('click','.clickToStyle',function(){ $('<link/>', {rel: 'stylesheet', href: 'style1.css'}).appendTo('head'); }) 获取的属性文本。

因此,您只需将属性的值更改为您想要的任何值。

data-tooltip
content: attr(data-tooltip)
function changeTooltip() {
  document.getElementById('abc').setAttribute('data-tooltip', 'aaa');
}

答案 1 :(得分:1)

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes 中所述,数据属性可以通过dataset对象访问,使用data-之后的属性名称部分。

在你的例子中:

&#13;
&#13;
var tlink = document.getElementById('abc');
 
tlink.dataset.tooltip = "def";
&#13;
/* 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;
}
&#13;
<a href="#" id="abc" data-tooltip="abc"><img src="http://www.w3schools.com/images/w3schools_green.jpg"/  ></a>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

extension UIButton
{
 func setUpLayer(sampleButton: UIButton?) {
  sampleButton!.setTitle("GET STARTED", forState: UIControlState.Normal)
  sampleButton?.tintColor =  UIColor.whiteColor()
  sampleButton!.frame = CGRect(x:50, y:500, width:170, height:40)
  sampleButton!.layer.borderWidth = 1.0
  sampleButton!.layer.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.5).CGColor
  sampleButton!.layer.cornerRadius = 5.0

  sampleButton!.layer.shadowRadius =  3.0
  sampleButton!.layer.shadowColor =  UIColor.whiteColor().CGColor
  sampleButton!.layer.shadowOpacity =  0.3
 }

}

小提琴 https://jsfiddle.net/omarCreativeDev/6rpxLu4p/