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
jquery length of text()
提问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:
很多事情是错误的:
- You didn't include jQuery in your page and you try to use functions from it
- You didn't close your
script
tag - Your div is completely broken
- You don't have a
DOCTYPE
- Your document doesn't have a
head
:-) - Your script is outside of the html
length
is a property not a function
- 您没有在页面中包含 jQuery 并且尝试使用其中的函数
- 你没有关闭你的
script
标签 - 你的div完全坏了
- 你没有
DOCTYPE
- 您的文档没有
head
:-) - 您的脚本在 html 之外
length
是属性而不是函数
回答by meder omuraliev
length
is not a function. it is a property.
length
是不是一个函数。它是一种财产。
'lol'.length === 3 // true
回答by Player1
You are looking for this function .length
But 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 可以处理一些函数。(夸张的意思)