0

要素の改行を検出するプラグインが必要です。コードは次のとおりです。

$.fn.inlineOffset = function (){
    if ($(this).css('text-indent') == '0px' && $(this).height() != 17) {
      var el = $('<i/>').css('display', 'inline').insertBefore(this[0]);
          pos = el.offset();
      el.remove();
      return pos;
    }
    else {
      var pos = $(this).offset()
      return pos;
    }
  };

ご覧のとおり、要素の高さが 17px の場合にのみ機能します。しかし、異なる高さを設定する必要がある場合はどうすればよいでしょうか? 新しい行の最初の文字の前に要素を追加しようとすると、親の左上隅に表示され、テキスト行が壊れている場合にのみ追加する必要があるため、この解決策が必要です。

4

2 に答える 2

0

私はその場で解決策を考え出しました。ここで試してみてください: http://jsfiddle.net/6V2Pd/1/ 重要なポイントは、次のコード行です。

//here we get the calculated height of the text element when white space is nowrap (single line)
$target.css('white-space', 'nowrap');
var nowrapHeight = $target.height();

//here we get the calc. height when white space is normal (breaks are allowed)
$target.css('white-space', 'normal');
var normalwrapHeight = $target.height();

// here we are going to compare the two values and come to a conclusion
// if nowrapHeight != normalwrapHeight => it's a multiline text
...
于 2013-11-04T10:49:17.700 に答える
0

私が使用した1つの方法は、テキストをスペースで配列に分割し、配列をスパンタグで結合して、すべてのスパンをループして位置を確認することにより、スパン内のすべての単語をラップすることです。top が増加すると、改行が見つかりました

于 2013-11-03T23:25:07.303 に答える