代码之家  ›  专栏  ›  技术社区  ›  Mauro74

jquery如何检索p标记中的字符数?

  •  2
  • Mauro74  · 技术社区  · 16 年前

    我的问题很简单:我想知道“p”标记中有多少个字符,如果数字超过100,则在悬停时触发一个事件。 我试着像这样检索字符号:

    var charLength = $('.myPar').val().length;
    <p class="myPar">this is the content of the paragraph</p>
    

    它总是返回0作为值,并且似乎只对textarea或input标记有效。

    毛罗

    2 回复  |  直到 16 年前
        1
  •  5
  •   balupton    16 年前

    $('.myPar:first').html().length $('.myPar:first').text().length

        2
  •  0
  •   Christian Smorra    16 年前

    var charLength = $('.myPar').text().length;