jquery文本长度()

van*_*van 6 html jquery

下面有什么问题

<html>
<body>
<div id='test')28</div>
</body>
</html>

<script>
$(document).ready(function (){
  if ($('#test').text().length() > 0) // error here?
   alert("test");
});
Run Code Online (Sandbox Code Playgroud)

我在IE 6中得到一个javascript错误,说明在标记错误的行上预期的功能..?

sus*_*ani 18

javascript中的长度不是函数.它的属性所以而不是length()使用长度


Dar*_*rov 12

很多事情都错了:

  1. 您没有在页面中包含jQuery,并尝试使用它中的函数
  2. 你没有关闭你的script标签
  3. 你的div完全崩溃了
  4. 你没有 DOCTYPE
  5. 你的文件没有head:-)
  6. 你的脚本不在html中
  7. length 属性不是函数


med*_*iev 7

length不是一个函数.这是一个属性.

'lol'.length === 3 // true
Run Code Online (Sandbox Code Playgroud)


Pla*_*er1 5

您正在寻找此功能.length但您需要在使用之前先添加文本值.length

回答: $("#test").val().length