[英]Truncate Text - Show full text in Tooltip on mouseover
Currently I output a text from my database with the following code: 当前,我使用以下代码从数据库中输出文本:
if ($data["own_subject"][$x]!="") { <td><p>".$data["own_subject"][0]."</p></td> }
I found a JS function to only show the first 10 characters and once someone does a mouseover the whole text appears. 我发现一个JS函数仅显示前10个字符,一旦有人将鼠标悬停在该文本上,就会显示整个文本。 This function is working with the following code and it is working fine:
此函数适用于以下代码,并且工作正常:
<script>
var lengthText = 10;
var text = $('p').text();
var shortText = $.trim(text).substring(0, lengthText).split(" ").slice(0, -1).join(" ") + "...";
$('p').text(shortText);
$('p').hover(function () {
$(this).text(text);
}, function () {
$(this).text(shortText);
});
</script>
Now I do not like the style of the outcome and I would like to show the full text in some kind of a tooltip. 现在,我不喜欢结果的样式,而是想以某种工具提示来显示全文。 I am using bootstrap and bootstrap has this function.
我正在使用引导程序,并且引导程序具有此功能。 My problem is now that I do not know how I need to change my JS code to show the full length text in a tooltip.
我的问题是,现在我不知道如何更改我的JS代码以在工具提示中显示完整长度的文本。 Can someone help me out and show me how I need to change my current code?
有人可以帮助我并向我展示如何更改当前代码吗?
I would really appreciate your any help. 非常感谢您的帮助。
Thanks, Chris 谢谢克里斯
text
in title attribute of p
tag which I hope you are already doing. text
添加到p
标签的title属性中。 data-toggle="tooltip" data-placement="top"
attributes to p
tag data-toggle="tooltip" data-placement="top"
属性添加到p
标签 Ex 防爆
<p data-toggle="tooltip" data-placement="top" title='".$data["own_subject"][0]."'>".$data["own_subject"][0]."</p>
$('[data-toggle="tooltip"]').tooltip()
Also you can now remove $('p').hover
event. $('[data-toggle="tooltip"]').tooltip()
初始化现在,您还可以删除$('p').hover
事件。 Ex 防爆
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
尝试使用标题的简单html工具提示。
if ($data["own_subject"][$x]!="") { <td><p title='".$data["own_subject"][0]."'>".$data["own_subject"][0]."</p></td> }
You can add title
attribute with real text to you element and call bootstraps
tooltip
init over that elements, also you need to remove current hover
handler from your script 您可以在元素中添加带有真实文本的
title
属性,并在该元素上调用bootstraps
tooltip
init,还需要从脚本中删除当前的hover
处理程序
$('p').text(shortText);
// Add title with real text to your element
$('p').attr('title', text);
// And all in document ready bootstrap tooltip init for your short text tags
$( document ).ready(function() {
$('p').tooltip();
});
check more about boostrap tolltip here: http://www.w3schools.com/bootstrap/bootstrap_ref_js_tooltip.asp 在此处查看有关Boostrap收费提示的更多信息: http : //www.w3schools.com/bootstrap/bootstrap_ref_js_tooltip.asp
You can do this using tooltip of bootstrap 您可以使用引导程序的工具提示来执行此操作
<button id="test" type="button" class="btn btn-default" data-toggle="tooltip" data-placement="bottom">
Tooltip on Bottom 底部的工具提示
$(function() {
var lengthText = 10;
var text = $('#test').text();
var shortText = $.trim(text).substring(0, lengthText).split(" ").slice(0, -1).join(" ") + "...";
$('#test').prop("title", text);
$('#test').text(shortText);
$('[data-toggle="tooltip"]').tooltip();
})
http://plnkr.co/edit/5hHRjULpDlMP3cYhHhU4?p=preview http://plnkr.co/edit/5hHRjULpDlMP3cYhHhU4?p=preview
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.