asp.net客户端$get和$find方法区别
$get 和 $find都是asp.net ajax注册到浏览器的前段javascript方法脚本。要使用$get 和 $find这2个javascript方法,aspx页面需要放置ScriptManager 。$get 和 $find的用法说明和异同如下
$get
$get等价于document.getElementById。当你在$get函数处打断点进行调试,你将会看到如下的代码
var $get = Sys.UI.DomElement.getElementById = function Sys$UI$DomElement$getElementById(id, element) { var e = Function._validateParams(arguments, [ {name: "id", type: String}, {name: "element", mayBeNull: true, domElement: true, optional: true} ]); if (e) throw e; if (!element) return document.getElementById(id); if (element.getElementById) return element.getElementById(id); var nodeQueue = []; var childNodes = element.childNodes; for (var i = 0; i < childNodes.length; i++) { var node = childNodes[i]; if (node.nodeType == 1) { nodeQueue[nodeQueue.length] = node; } } while (nodeQueue.length) { node = nodeQueue.shift(); if (node.id == id) { return node; } childNodes = node.childNodes; for (i = 0; i < childNodes.length; i++) { node = childNodes[i]; if (node.nodeType == 1) { nodeQueue[nodeQueue.length] = node; } } } return null; }
通过上面的代码知道,函数的签名如下
$get(id, element)
方法的id参数类型为字符串,为DOM元素的id。参数element可选(DOM元素),如果传递了这个对象参数,判断对象是否支持getElementById,支持则调用此方法在此对象中查找id指定的对象。否则遍历此对象的所有子元素,找到子元素id和id参数一致的子元素返回。$get示例代码如下
<form id="form1" runat="server"> <div id="contentDiv"> <asp:DropDownList ID="ddCountries" runat="server"> </asp:DropDownList> <asp:TextBox ID="txtChange" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> </form> <script>//通过 ClientID属性获取ASP.NET 服务器端控件的id var txtCntl = $get('<%=txtChange.ClientID%>'); //获取一个不是服务器端的控件 var divCntl = $get('contentDiv'); //需要在哪个对象下查找id指定的元素 var dropDownCntl = $get('<%=ddCountries.ClientID %>', divCntl); </script>
上面代码展示了如何使用¥get获取控件,示例代码中使用了内联服务器端代码<%%>( inline server code )用于获取asp.net系统生成的服务器端控件对应的客户端控件id。第二行代码使用了Div标签的id获取控件。最后一行则是查找div下的指定id的元素。
$find
$find等价于Sys.Application.findComponent(id, parent)。函数参数id为要查找的元素id。第二个参数可选,可以为组件或者dom元素。函数签名如下
$find(id, parent)
返回被包装过的对象,打断点后得到如下代码
function Sys$_Application$findComponent(id, parent) { var e = Function._validateParams(arguments, [ {name: "id", type: String}, {name: "parent", mayBeNull: true, optional: true} ]); if (e) throw e; // 需要直接参考应用程序单实例,因为$find指向没有上线文的实例函数。不要使用this对象。 //Need to reference the application singleton directly beause the $find alias // points to the instance function without context. The 'this' pointer won't work here. return (parent ? ((Sys.IContainer.isInstanceOfType(parent)) ? parent.findComponent(id) : parent[id] || null) : Sys.Application._components[id] || null); }
上面的代码检查parent 参数是否为 IContainer 类型,如果是将parent组件中找id指定的组件对象,否则返回Sys.Application的 _components数组中的对象。
$get和$find在哪时使用
$get返回DOM对象, $find返回扩展过的对象。如果使用$get获取扩展对象,如ValidatorCalloutExtender,AutoCompleteExtender 等将返回null。这就是 $get 和 $find的区别。
翻译来源:http://sandblogaspnet.blogspot.com/2009/06/difference-between-get-and-find.html
加支付宝好友偷能量挖...
原创文章,转载请注明出处:asp.net客户端$get和$find方法区别