av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

java語言

jQuery實(shí)現(xiàn)計(jì)算頁面某段文字的長度的函數(shù)

時(shí)間:2025-04-19 12:21:42 java語言 我要投稿
  • 相關(guān)推薦

jQuery實(shí)現(xiàn)計(jì)算頁面某段文字的長度的函數(shù)

  如何計(jì)算頁面某段文字的長度呢?我們?nèi)绾瓮ㄟ^jQuery 來實(shí)現(xiàn)呢?下面是小編給大家提供的實(shí)現(xiàn)函數(shù)代碼,歡迎閱讀,更多詳情請(qǐng)關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。

  jQuery計(jì)算頁面某段文字的長度的函數(shù):核心函數(shù),注意是基于JQuery。這個(gè)辦法計(jì)算的值,無法大于頁面寬度。

  Javascript代碼:

  var stringWidth = function(fontSize, content) {

  var $span = $('<span></span>').hide().css('font-size', fontSize).text(content);

  var w = $span.appendTo('body').width();

  $span.remove();

  return w;

  };

  一個(gè)頁面的完整例子,請(qǐng)打開控制臺(tái)看結(jié)果:

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  <html>

  <head>

  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

  <title>Document</title>

  <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

  <script>

  var stringWidth = function(fontSize, content) {

  var $span = $('<span></span>').hide().css('font-size', fontSize).text(content);

  var w = $span.appendTo('body').width();

  $span.remove();

  return w;

  };

  $(function(){

  if(console && console.log){

  console.log(stringWidth('12px', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia, quos repellat maiores doloribus laborum natus quo laboriosam illum hic deleniti. Laborum, quisquam eaque sapiente quasi minus eveniet officiis vel perferendis!'));

  }

  });

  </script>

  <style type="text/css">

  </style>

  </head>

  <body>

  </body>

  </html>

【jQuery實(shí)現(xiàn)計(jì)算頁面某段文字的長度的函數(shù)】相關(guān)文章:

PHP中實(shí)現(xiàn)頁面跳轉(zhuǎn)07-06

PHP頁面跳轉(zhuǎn)實(shí)現(xiàn)技巧04-23

php頁面緩存實(shí)現(xiàn)方法07-20

jQuery Mobile + PHP實(shí)現(xiàn)文件上傳03-20

wps文字如何布局頁面07-13

PHP頁面跳轉(zhuǎn)幾種實(shí)現(xiàn)技巧07-26

php指定長度分割字符串str-split函數(shù)如何實(shí)現(xiàn)06-15

如何使用JavaScript實(shí)現(xiàn)頁面定時(shí)跳轉(zhuǎn)04-02

獲取php數(shù)組長度函數(shù)的方法06-05