【问题标题】:Dynamically change tool tip text动态更改工具提示文本
【发布时间】:2018-01-30 11:11:59
【问题描述】:

首先,我还是个初学者。我做了一个测试表。当我将鼠标悬停在表格上时,我想动态更改工具提示的文本。例如,我想获取表格单元格的内容并将其作为工具提示的文本。到目前为止,我只有一个静态文本。在functionMouseOver() 中,我需要获取单元格的内容并将其作为文本放在工具提示中。我找不到执行此操作的 JavaScript 方法。我的代码如下。感谢您的帮助。

function functionMouseOver(id) {
  document.getElementById(id).style.backgroundColor = "rgb(0,255,255)";
}

function functionMouseOut(id) {
  document.getElementById(id).style.backgroundColor = "rgb(255,255,255)";
}

// create HTML Table body
titlenames = ["Jan", "Feb", "Mar"]
var HTMLTableBody = "";
HTMLTableBody += "<div class='container'><table id='id1' style='width:40%'>";
HTMLTableBody += "<tr>";
for (var i = 0; i < 3; i++) {
  HTMLTableBody += "<th id='th" + i + "' class='class1'>" + titlenames[i] + "</th>";
}
HTMLTableBody += "</tr>";
var cnt = 0;
for (var i = 0; i < 2; i++) {
  HTMLTableBody += "<tr>";
  for (var j = 0; j < 3; j++) {
    idstr = "td" + cnt;
    HTMLTableBody += "<td id='td" + cnt + "' class='class2'" +
      ' onMouseOver =' + '"functionMouseOver(' + "'" + idstr + "'" + ')"' +
      ' onMouseOut =' + '"functionMouseOut(' + "'" + idstr + "'" + ')"' +
      ' data-container =' + '"body"' +
      ' data-placement=' + '"right"' +
      ' data-toggle=' + '"tooltip"' +
      ' title=' + '"text"' +
      "></td > ";
    cnt++;
  }
  HTMLTableBody += "</tr>";
}
HTMLTableBody += "</table></div>";

document.getElementById("HTMLBody").innerHTML = "" + HTMLTableBody;

// fill table with data
cnt = 0;
for (var i = 0; i < 2; i++) {
  for (var j = 0; j < 3; j++) {
    var strid = "td" + cnt;
    document.getElementById(strid).innerHTML = cnt;
    cnt++;
  }
}

$(document).ready(function() {
  $('[data-toggle="tooltip"]').tooltip();
});
.class2 {
  background-color: rgb(255, 255, 255);
  color: rgb(0, 0, 0);
  text-align: center;
  font-size: 36px;
  font-family: "Lucida Console", Monaco, monospace;
}

.class1 {
  background-color: rgb(200, 100, 200);
  color: rgb(255, 255, 255);
  text-align: center;
  font-size: 36px;
  font-family: "Lucida Console", Monaco, monospace;
}

table {
  border-spacing: 1px;
  border-collapse: collapse;
  overflow: hidden;
  z-index: 1;
}

td {
  cursor: pointer;
  padding: 10px;
  position: relative;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<body id="HTMLBody">
</body>

【问题讨论】:

  • 您可以在创建它们时设置标题,即' title=' + '"' + cnt + '"' +
  • 谢谢,是的,在这种情况下会起作用,但我正在处理预订日历,不确定是否会起作用,但我会再看看。也许我可以在创建 HTML 框架之前确定文本需要是什么。然而,在 HTML 框架已经构建之后如何操作它呢?补充说明:当我在我的计算机上运行代码时,它看起来与我在论坛上“运行代码 sn-p”看到的不同

标签: javascript jquery html css twitter-bootstrap


【解决方案1】:

您可以像下面这样更新functionMouseOver(),而不是在开头设置所有标题:

function functionMouseOver(id) {
  document.getElementById(id).style.backgroundColor = "rgb(0,255,255)";  
  document.getElementById(id).setAttribute("data-original-title",document.getElementById(id).innerHTML);
}

function functionMouseOut(id) {
  document.getElementById(id).style.backgroundColor = "rgb(255,255,255)";
}

// create HTML Table body
titlenames = ["Jan", "Feb", "Mar"]
var HTMLTableBody = "";
HTMLTableBody += "<div class='container'><table id='id1' style='width:40%'>";
HTMLTableBody += "<tr>";
for (var i = 0; i < 3; i++) {
  HTMLTableBody += "<th id='th" + i + "' class='class1'>" + titlenames[i] + "</th>";
}
HTMLTableBody += "</tr>";
var cnt = 0;
for (var i = 0; i < 2; i++) {
  HTMLTableBody += "<tr>";
  for (var j = 0; j < 3; j++) {
    idstr = "td" + cnt;
    HTMLTableBody += "<td id='td" + cnt + "' class='class2'" +
      ' onMouseOver =' + '"functionMouseOver(' + "'" + idstr + "'" + ')"' +
      ' onMouseOut =' + '"functionMouseOut(' + "'" + idstr + "'" + ')"' +
      ' data-container =' + '"body"' +
      ' data-placement=' + '"right"' +
      ' data-toggle=' + '"tooltip"' +
      "></td > ";
    cnt++;
  }
  HTMLTableBody += "</tr>";
}
HTMLTableBody += "</table></div>";

document.getElementById("HTMLBody").innerHTML = "" + HTMLTableBody;

// fill table with data
cnt = 0;
for (var i = 0; i < 2; i++) {
  for (var j = 0; j < 3; j++) {
    var strid = "td" + cnt;
    document.getElementById(strid).innerHTML = cnt;
    cnt++;
  }
}

$(document).ready(function() {
  $('[data-toggle="tooltip"]').tooltip();
});
.class2 {
  background-color: rgb(255, 255, 255);
  color: rgb(0, 0, 0);
  text-align: center;
  font-size: 36px;
  font-family: "Lucida Console", Monaco, monospace;
}

.class1 {
  background-color: rgb(200, 100, 200);
  color: rgb(255, 255, 255);
  text-align: center;
  font-size: 36px;
  font-family: "Lucida Console", Monaco, monospace;
}

table {
  border-spacing: 1px;
  border-collapse: collapse;
  overflow: hidden;
  z-index: 1;
}

td {
  cursor: pointer;
  padding: 10px;
  position: relative;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<body id="HTMLBody">
</body>

【讨论】:

  • 非常感谢。我想这就是我所追求的。今天下午玩,现在要出去几个小时
  • 好吧,是的,我不知道。我刚才添加了一个“向上箭头”投票,但它说我的信用太少了。但是您的解决方案有效。那里有很多东西。您是如何找到名称“data-original-title”的?您是否查看了 tooltip.js 文档?我像 2 周前一样开始使用 javascript,我主要是在谷歌上搜索信息。您有什么方法可以快速找到编码问题的解决方案?
  • 我已经使用 JS 多年了。知道这些东西是如何工作的。没有直接的方法来实际找出快速解决方案。但这个平台非常适合所有经验水平的开发人员。而且我相信您需要 15 Reputation 才能投票给答案:(
  • 我也编码了很长时间,但只有第 4 代语言和软件特定语言。如果您不介意,我有 1 个后续问题。我使用此处解释的工具提示设置:w3schools.com/bootstrap/bootstrap_ref_js_tooltip.asp 现在我尝试更改“放置”。我需要什么名字? “数据放置”不起作用,“放置也不起作用。我使用的文本字符串也添加了“\n”换行符。但是它们没有显示在文本中。要将位置更改为顶部,我尝试了:文档。 getElementById(id).setAttribute("data-placement", "top"); 不起作用
  • 在标题中找到了换行符的答案。在这里:stackoverflow.com/questions/19001766/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 2013-12-04
相关资源
最近更新 更多