jquery 文本长度()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3282953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:10:45  来源:igfitidea点击:

jquery length of text()

jqueryhtml

提问by van

What's wrong with the below

下面有什么问题

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

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

I get a javascript error in I.E 6 saying function expected on the line marked error..?

我在 IE 6 中收到一个 javascript 错误,说在标记为错误的行上期望函数..?

回答by sushil bharwani

Length in javascript is not a function. its a property so instead of length() use length

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

回答by Darin Dimitrov

Manythings are wrong:

很多事情是错误的:

  1. You didn't include jQuery in your page and you try to use functions from it
  2. You didn't close your scripttag
  3. Your div is completely broken
  4. You don't have a DOCTYPE
  5. Your document doesn't have a head:-)
  6. Your script is outside of the html
  7. lengthis a property not a function
  1. 您没有在页面中包含 jQuery 并且尝试使用其中的函数
  2. 你没有关闭你的script标签
  3. 你的div完全坏了
  4. 你没有 DOCTYPE
  5. 您的文档没有head:-)
  6. 您的脚本在 html 之外
  7. length是属性而不是函数

回答by meder omuraliev

lengthis not a function. it is a property.

length不是一个函数。它是一种财产。

'lol'.length === 3 // true

回答by Player1

You are looking for this function .lengthBut you need to add the text value first before using .length.

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

Answer: $("#test").val().length

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

回答by zfm

beside the length(), there is another mistake

在旁边length(),还有一个错误

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

回答by Ben Guthrie

Don't use JQuery to get length. Just use the standard javascript .length command.

不要使用 JQuery 来获取长度。只需使用标准的 javascript .length 命令。

There are a few functions that Javascript can handle without JQuery's help. (hyperbole intended)

在没有 JQuery 的帮助下,Javascript 可以处理一些函数。(夸张的意思)