日期:2014-05-16 浏览次数:20430 次
<head> <title>DOM对象与jQuery对象的类型转换</title> <script type="text/javascript" src="Jscript/jquery-1.4.2-vsdoc.js"> </script> <script type="text/javascript" src="Jscript/jquery-1.4.2.js"> </script> <style type="text/css"> body{font-size:13px} div{padding:5px} </style> <script type="text/javascript"> $(function() { //***** DOM对象转成jQuery对象 *****// //DOM对象 var objDom0 = document.getElementById("div0"); //转成jQuery对象 var $obj0 = $(objDom0); //调用jQuery中的方法设置其中的内容 $obj0.html("DOM对象转成jQuery对象后设置的内容"); //***** jQuery对象转成DOM对象 *****// //jQuery对象 var $obj1 = $("#div1"); //转成DOM对象 var objDom1 = $obj1.get(0); //调用JavaScript中的对象方法设置内容 objDom1.innerHTML = "jQuery对象转成DOM对象后设置的内容"; }) </script> </head> <body> <div id="div0"></div> <div id="div1"></div> </body> </html>
function toJson(obj){ switch(obj.constructor){ case Object: var str = "{"; for(var o in obj){ str += o + ":" + Serialize(obj[o]) +","; } www.2cto.com if(str.substr(str.length-1) == ",") str = str.substr(0,str.length -1); return str + "}"; break; case Array: var str = "["; for(var o in obj){ str += Serialize(obj[o]) +","; } if(str.substr(str.length-1) == ",") str = str.substr(0,str.length -1); return str + "]"; break; case Boolean: return "\"" + obj.toString() + "\""; break; case Date: return "\"" + obj.toString() + "\""; break; case Function: break; //如果是方法,则过滤掉,什么也不做 case Number: return "\"" + obj.toString() + "\""; break; case String: return "\"" + obj.toString() + "\""; break; } }