Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 截断文本并将其显示在鼠标上方_Javascript_Jquery_Html - Fatal编程技术网

Javascript 截断文本并将其显示在鼠标上方

Javascript 截断文本并将其显示在鼠标上方,javascript,jquery,html,Javascript,Jquery,Html,我需要截短文本(结尾是…),然后在鼠标上,整个文本应该展开 我试图用下面的代码截断。这段代码的问题是,它会在点击…时扩展内容,但我需要在用户将鼠标移到p标记上的任何位置时打开它 var len = 100; var p = document.getElementById('truncateMe'); if (p) { var trunc = p.innerHTML; if (trunc.length > len) { trunc = trunc.substring(0,

我需要截短文本(结尾是…),然后在鼠标上,整个文本应该展开

我试图用下面的代码截断。这段代码的问题是,它会在点击
时扩展内容,但我需要在用户将鼠标移到p标记上的任何位置时打开它

var len = 100;
var p = document.getElementById('truncateMe');
if (p) {

  var trunc = p.innerHTML;
  if (trunc.length > len) {

    trunc = trunc.substring(0, len);
    trunc = trunc.replace(/\w+$/, '');

    trunc += '<a href="#" ' +
      'onmouseover="this.parentNode.innerHTML=' +
      'unescape(\''+escape(p.innerHTML)+'\');return false;">' +
      '...<\/a>';
    p.innerHTML = trunc;
  }
}
var len=100;
var p=document.getElementById('truncateMe');
如果(p){
var trunc=p.innerHTML;
如果(实际长度>长度){
trunc=trunc.子串(0,len);
trunc=trunc.replace(/\w+$/,“”);
trunc+='

我正在寻找一个简单的方法来做这件事

提前谢谢


PS:请不要使用CSS解决方案,因为它与所有浏览器(IE7)都不兼容。

您可以这样使用Jquery:

HTML:

<p>Some Text</p>
演示:

或者您也可以使用css3属性
文本溢出:省略号;

CSS:

p{
    text-overflow:ellipsis;
    width: 250px;
    white-space: nowrap;
    overflow: hidden;
}

p:hover{
    text-overflow:clip;
    width:auto;
    white-space: normal;
}

演示:

假设您将
p
-元素的类设置为
转义文本
,下面的jQuery代码应该可以工作

$(document).ready(function() {
    $ps = $('.escape-text');


    $ps.each(function(i, el) {
        $(el).data('full-text', el.innerHTML);

        strip(el);
    });

    $ps.on('mouseover', function() {
        $(this).text($(this).data('full-text'));
    }).on('mouseout', function() {
        $(this).text(strip(this));
    })

});

var length = 100;
var strip = function(el) {
    el.innerHTML = el.innerHTML.substr(0, length) + '...';
}

什么是
escape
?你能提供一个演示吗?我只是想建议一下-我不知道他想让它为什么浏览器工作,因为他说css不兼容所有浏览器,这将兼容回IE8是的,它是一个新属性css3,所以不能在旧浏览器上工作,但它可以从IE8+上工作:是的,这就是我为什么选择它的原因d在问题中提到没有CSS解决方案请确认抱歉,我已经用Jquery解决方案更新了我的帖子。就是这样。谢谢:)这是因为JSFIDLE有自己的onload方法,并且长度/带函数对于我用于
$ps
的内部函数不可用。将此代码应用到您自己的项目中,您将看到我它不起作用。
$(document).ready(function() {
    $ps = $('.escape-text');


    $ps.each(function(i, el) {
        $(el).data('full-text', el.innerHTML);

        strip(el);
    });

    $ps.on('mouseover', function() {
        $(this).text($(this).data('full-text'));
    }).on('mouseout', function() {
        $(this).text(strip(this));
    })

});

var length = 100;
var strip = function(el) {
    el.innerHTML = el.innerHTML.substr(0, length) + '...';
}