ASP.NET开发者使用jQuery应该了解的几件事情
下面这几点希望能够帮你加快你的学习。
1.把selectors当作asp.net中的sets。
在asp.net的世界,通过一个查询找到一系列控件是非常少见的.相反我们比较习惯于通过一个唯一的id来找到一个控件。当然,在jquery中这个也是没有问题的,但是jquery的选择功能相对更加神奇。
使用jquery的selectors能够很容易定位到一个set的元素,相对来说比在asp.net中使用迭代的规则来找到一个set的元素更加清晰和易于表达。
2.使用css类来代替styling。
另外一个不直观的技术是把css类作为一个flag。与'selector engin'一样,'flag'类也是个令人惊喜的东西。
举个例子来说吧,最近有个活动是做一个在线的能够与客户端互动的纸牌游戏。一个需求是纸牌需要有个onclick haddler在某个特定时间,不过这些事件只针对那些face down(脸朝下)的。作为一个.net的开发者我立刻能够想到的办法是让这些纸牌在客户端通过一个collection来编号.然后我可以在需要的时候给这个数据一个onclick handlers。这个固然可以,不过难以维护且有点凌乱。
现在如果我使用css类来实现,face up的纸牌我通过addclass方法为它们加上一个'flipped'类,然后可以通过一个简单的 '$(".card:not(.flipped)")'选择那些face down的纸牌。使用jquery的click(fn)功能能够让我使用几行代码就实现这个功能。更重要的是它更容易理解和读取。
下面附上几点英文原版的:
3。understand unobtrusive javascript.
in the asp.net world, we use a lot of what's sometimes termed obtrusive javascript. this means that client-side event handlers are defined as attributes on elements. for example, several asp.net webcontrols render an onclick=”javascript:__dopostback()”attribute as part of their markup. this is considered obtrusive javascript.
when asp.net was initially being developed, this inline javascript was the norm. however, as browsers began providing more sophisticated faculties for imperatively adding event handlers, this declarative technique quickly lost favor with client-side developers. as a consequence, the preferred approach has shifted toward what's called unobtrusive javascript.
unobtrusive javascript is now considered a best practice when wiring up client-side event handlers. this is primarily because it facilitates separation of concerns between behavioral javascript and structural html markup. unobtrusive javascript also helps you to write cleaner, more semantic markup, which improves accessibility and often has seo benefits.
coming from the save-compile-reload paradigm of statically typed server-side development, it's natural to approach client-side development in a similar fashion. while you certainly can write client-side code that way, it's akin to working blindfolded when you consider the alternatives.
since javascript is usually interpreted by a browser, the browser is one of the best debugging environments available. in particular, a javascript “console” is terrific for interactively interrogating the dom, testing jquery selectors against actual markup, and refining javascript code in real-time.
my preferred browser-based tool is the firebug addon to firefox. i cannot praise this firebug highly enough. it has revolutionized how i approach client-side development, both of javascript and of css. if you prefer internet explorer, ie8's updated developer tools are also very capable in this department.
whatever your browser of choice, i urge you to give these utilities a try when debugging client-side functionality. once you become proficient with one of these tools, you'll be amazed that you ever developed client-side code without it.
even though browser-based tools are great for debugging, an asp.net developer's primary editor is still going to be visual studio. when writing jquery code in visual studio, having proper intellisense can make a tremendous difference in productivity. the discoverability that intellisense provides is especially beneficial when you're unfamiliar with jquery's api.
as part of the official support for jquery, microsoft provides a documentation file to provide jquery intellisense inside visual studio 2008. this is provided through what's called a vsdoc file, and is available on the jquery download page (via the “documentation: visual studio” links).
jeff king has assembled an excellent faq to help you get visual studio 2008's javascript intellisense working: http://blogs.msdn.com/webdevtools/archive/2008/11/18/jscript-intellisense-faq.aspx
上一篇: ASP.NET 页面之间传递参数方法汇总
下一篇: Java 静态绑定与动态绑定深入分析