[英]Show “Today Date” in the “Title Attribute” of a tooltip – Bootstrap
我用JS调用今天的日期
<script>
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var n = new Date();
var y = n.getFullYear();
var m = n.getMonth();
var d = n.getDate();
document.getElementById("today").innerHTML = d + " " + months[m] + " " + y;
</script>
现在我试图在工具提示中使用这个字符串作为标题属性(引导程序)。
<span class="font-size-1" data-toggle="tooltip" data-html="true" title="here should go the string TODAY">Today</span>
它只是行不通。 有谁看到这个问题吗? 如何在标题中添加 id="today" 或基本上显示在工具提示中?
PS。 如果我将它嵌套在 P 中,日期会显示得很好(所以 JS 很好):
<p id="today"></p>
所以你想在跨度的工具提示上显示日期,对吧? 只需将标题设置为today
并给跨度一个id
。 这对你有用吗?
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var n = new Date(); var y = n.getFullYear(); var m = n.getMonth(); var d = n.getDate(); document.getElementById("today").title = d + " " + months[m] + " " + y;
<span class="font-size-1" data-toggle="tooltip" data-html="true" id="today">Today</span>
使用title
作为 function。 我还使用toLocaleString()
缩短了您的日期计算:
$('span.custom').tooltip({ title: function() { return new Date().toLocaleString("en-GB", {day: '2-digit', month: 'long', year: 'numeric'}) } })
body { padding: 25px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" /> <span class="custom font-size-1 font-weight-bold" data-toggle="tooltip" data-html="true">Today</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.