JQuery对象的val()方法执行结果分析
JavaScript中,如果id对应的标签不存在(同理JAVA中,如果对象不存在),则调用它的方法会报错或抛异常。在实际开发中,发现JQuery在id对应的标签不存在时,调其val()方法不会报错,结果是undefined。
实例如下:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>jQuery UI Accordion - Collapse content</title> <link rel="stylesheet" href="lib/ui/1.10.2/themes/jquery-ui.css"> <script src="lib/jquery/jquery-1.9.1.js"></script> <script> $(function() { var test1 = $("#test1").val(); alert(test1); //bijian var test2 = $("#test2").val(); alert(test2); //"" var test3 = $("#test3").val(); alert(test3); //undefined }); </script> </head> <body> <input id="test1" type="text" value="bijian"/> <input id="test2" type="text"/> </body> </html>
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17