2011-11-22 56 views
0
$("textarea").cleditor({ width: "800px", height: "300px" })[0].focus(); 

,但是当我没有textarea的felad我得到如何检查textarea字段是否存在与jQuery或JavaScript的窗体?

Error: $("textarea").cleditor({width: "800px", height: "300px"})[0] is undefined 

我怎么能检查是否textarea的领域存在。我尝试($(“textarea”)。val())但不能正常工作。我也尝试价值(),但也没有工作。我如何检查是否存在?

+1

重复的http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery – v42

回答

5

使用lengthjQuery对象的属性,如果它找到任何元素,则为非零。试试这个

if($("textarea").length > 0){ 
    //your code here 
} 
+0

这很快。谢谢 :) – senzacionale

0

var $ textArea = $(“textarea”); ($ textArea.length> 0){text_rea.cleditor({width:“800px”,height:“300px”})[0] .focus(); }

相关问题