亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何阻止 JS 代碼將空格包裝成跨度?

如何阻止 JS 代碼將空格包裝成跨度?

PHP
拉莫斯之舞 2022-10-14 14:33:21
我從“ Detect browser Wrapped lines via javascript ”中找到了 JS 代碼,但它會將空格包裝成一個跨度。我在 WordPress 網站上使用此代碼,我無法在其他任何地方解決這個問題。我試圖清理代碼,但效果不佳。有沒有辦法檢查跨度是否為空白并刪除跨度?$(".emails .multi-items .content").each(function() {  var $cont = $(this);  var text_arr = $cont.text().split(' ');  for (i = 0; i < text_arr.length; i++) {    text_arr[i] = '<span>' + text_arr[i] + ' </span>';  }  $cont.html(text_arr.join(''));  $wordSpans = $cont.find('span');  var lineArray = [],    lineIndex = 0,    lineStart = true,    lineEnd = false  $wordSpans.each(function(idx) {    var pos = $(this).position();    var top = pos.top;    if (lineStart) {      lineArray[lineIndex] = [idx];      lineStart = false;    } else {      var $next = $(this).next();      if ($next.length) {        if ($next.position().top > top) {          lineArray[lineIndex].push(idx);          lineIndex++;          lineStart = true        }      } else {        lineArray[lineIndex].push(idx);      }    }  });  //console.log( lineArray)  for (i = 0; i < lineArray.length; i++) {    var start = lineArray[i][0],      end = lineArray[i][1] + 1;    /* no end value pushed to array if only one word last line*/    if (!end) {      $wordSpans.eq(start).wrap('<span class="line_wrap">')    } else {      $wordSpans.slice(start, end).wrapAll('<span class="line_wrap">');    }  };});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="emails"><ul class="multi-items"><div class="content">                             I’m up to something. Learning is cool, but knowing is better, and I know the key to success. Bless up. They never said winning was easy. Some people can’t handle success, I can.I told you all this before, when you have a swimming pool, do not use chlorine, use salt water, the healing, salt water is the healing. Put it this way, it took me twenty five years to get these plants, twenty five years of blood sweat and tears, and I’m never giving up, I’m just getting started.</div></ul></div>
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

您可以檢查被包裝的內容是否不為空。


for (i = 0; i < text_arr.length; i++) {

    if (text_arr[i].trim().length > 0) {

        text_arr[i] = '<span>' + text_arr[i] + ' </span>';

    }

}

這是編輯后的片段:


$(".emails .multi-items .content").each(function() {

  var $cont = $(this);


  var text_arr = $cont.text().split(' ');


  for (i = 0; i < text_arr.length; i++) {

      if (text_arr[i].trim().length > 0) {

          text_arr[i] = '<span>' + text_arr[i] + ' </span>';

      }

  }


  $cont.html(text_arr.join(''));


  $wordSpans = $cont.find('span');


  var lineArray = [],

    lineIndex = 0,

    lineStart = true,

    lineEnd = false


  $wordSpans.each(function(idx) {

    var pos = $(this).position();

    var top = pos.top;


    if (lineStart) {

      lineArray[lineIndex] = [idx];

      lineStart = false;


    } else {

      var $next = $(this).next();


      if ($next.length) {

        if ($next.position().top > top) {

          lineArray[lineIndex].push(idx);

          lineIndex++;

          lineStart = true

        }

      } else {

        lineArray[lineIndex].push(idx);

      }

    }


  });



  //console.log( lineArray)

  for (i = 0; i < lineArray.length; i++) {

    var start = lineArray[i][0],

      end = lineArray[i][1] + 1;


    /* no end value pushed to array if only one word last line*/

    if (!end) {

      $wordSpans.eq(start).wrap('<span class="line_wrap">')

    } else {

      $wordSpans.slice(start, end).wrapAll('<span class="line_wrap">');

    }


  };

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="emails">

<ul class="multi-items">

<div class="content">

                             I’m up to something. Learning is cool, but knowing is better, and I know the key to success. Bless up. They never said winning was easy. Some people can’t handle success, I can.

I told you all this before, when you have a swimming pool, do not use chlorine, use salt water, the healing, salt water is the healing. Put it this way, it took me twenty five years to get these plants, twenty five years of blood sweat and tears, and I’m never giving up, I’m just getting started.

</div>

</ul>

</div>


查看完整回答
反對 回復 2022-10-14
  • 1 回答
  • 0 關注
  • 116 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號