在text()上使用jquery限制字符串

时间:2018-03-16 10:00:16

标签: jquery substring substr

我遇到了jquery的问题。

我的代码:

$("#MonthsDataTotal").text(obj.info["MonthsDataTotal"] / 1024);

所以我需要像这样设置子字符串:

$("#MonthsDataTotal").text(obj.info["MonthsDataTotal"] / 1024).substring(0,5);

我想在线,我发现了substring或substr方法,但这对我不起作用。 这是一个简单的问题,但我找不到解决方案。 如何使用jquery的text()方法设置打印限制字符串?

感谢。

1 个答案:

答案 0 :(得分:0)

您可以substring一个号码,您需要将其转换为包含toStringString()或甚至(不那么漂亮的方式)的字符串添加引号标志:

//toString(): 
(obj.info["MonthsDataTotal"] / 1024).toString();

//String():
String(obj.info["MonthsDataTotal"] / 1024);

//adding '' or "":
((obj.info["MonthsDataTotal"] / 1024) + '')
((obj.info["MonthsDataTotal"] / 1024) + "")

您可以使用slicesubstr代替子字符串 注意:substring和substr具有不同的行为



var num = 99999999;

//division without conversion
$("#wconv").text(num / 1024);

//conversions to string
var tostr = (num / 1024).toString();
var str = String(num / 1024);
var sq = (num / 1024) + '';
var dq = (num / 1024) + "";

//showing conversions
$("#tostr").text(tostr);
$("#str").text(str);
$("#sq").text(sq);
$("#dq").text(dq);

//using substring
$("#tostrsub").text(str.substring(0,5));
$("#strsub").text(str.substring(0,5));
$("#sqsub").text(sq.substring(0,5));
$("#dqsub").text(dq.substring(0,5));

//using slice
$("#tostrsli").text(str.slice(0,5));
$("#strsli").text(str.slice(0,5));
$("#sqsli").text(sq.slice(0,5));
$("#dqsli").text(dq.slice(0,5));

//using substr
$("#tostrsbr").text(str.substr(0,5));
$("#strsbr").text(str.substr(0,5));
$("#sqsbr").text(sq.substr(0,5));
$("#dqsbr").text(dq.substr(0,5));

table{
  border: 1px solid black;
  text-align: center;
}

th {
  min-width: 100px;
}

thead tr th{
  border-bottom: 1px solid black;
}

tfoot tr th, tfoot tr td{
  border-top: 1px solid black;
}

td, th {
  border-right: 1px solid black;
}

td:last-child, th:last-child{
  border-right: 0px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
  <thead>
    <tr>
      <th></th>
      <th>Converted</th>
      <th>Substring</th>
      <th>Slice</th>
      <th>Substr</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>toString()</th>
      <td><span id="tostr"></span></td>
      <td><span id="tostrsub"></span></td>
      <td><span id="tostrsli"></span></td>
      <td><span id="tostrsbr"></span></td>
    </tr>
    <tr>
      <th>String()</th>
      <td><span id="str"></span></td>
      <td><span id="strsub"></span></td>
      <td><span id="strsli"></span></td>
      <td><span id="strsbr"></span></td>
    </tr>
    <tr>
      <th>Single quotes</th>
      <td><span id="sq"></span></td>
      <td><span id="sqsub"></span></td>
      <td><span id="sqsli"></span></td>
      <td><span id="sqsbr"></span></td>
    </tr>
    <tr>
      <th>Double quotes</th>
      <td><span id="dq"></span></td>
      <td><span id="dqsub"></span></td>
      <td><span id="dqsli"></span></td>
      <td><span id="dqsbr"></span></td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th>Number / 1024: </th>
      <td colspan="4"><span id="wconv"></span></td>
    </tr>
   </tfoot>
</table>
&#13;
&#13;
&#13;