Powering Javascript UI with CSS

If you have been doing any Javscript coding during the current Javascript renaissance, you will have noticed that a lot of the user interface functionality you build is hiding and showing things.

After a while of this you might find yourself coding up toggle functions, which find elements and toggle their visibility. togglePair, toggleVisibilityById, there are lots of these types of functions in javascript apps today.

I've been trying out a different way of dealing with the need to change the UI based on user inputs or asynchronous data events: CSS.

With CSS, you can use the DOM as a data model and then use declarative rules to establish the display. It's cleaner, because you limit your javascript code to changing the DOM's data instead of it's UI, and then you use the extensive abilities of CSS to handle the visual representation of that data.

Here's an example. A common thing to do with javascript seems to be expanding out a form to fill out. Livejournal does this with their login form, or 37Signal's tadalist uses this when you want to add an item to your todo list.

here's the just javascript way to do this:

<div>
<span onClick='document.getElementById("form1").style.display="block";'>click me to add a comment</span>
</div>
...
<form id='form1'>
<textarea>add your comment</textarea>
</form>

But here's the CSS + Javascript way to do it.
CSS Rules

form#addComment {

display:none;

}

form#addComment.editing {
display:block;
}

Javascript + HTML

<div>
<span onClick='document.getElementById("addComment").setAttribute("class","editing");'>click me to show form</span>
</div>
...
<form id='addComment'>
<textarea>fill in some data here</textarea>
</form>

This can prove really advantageous when you are radically changing the visual presentation of the page. Instead of finding and changing the style of tons of elements by id, you can change 1 element's class and then use the inheritance of CSS to style all the sub-elements.

AddThis Social Bookmark Button

相关文档(Relevant Entries)
CSS缩写技巧
如何敏捷你的用户界面开发
预览待上传的本地图片
DIV+CSS网页布局常用的方法与技巧
采用DIV+CSS布局的好处
用CSS进行网页样式设计攻略全集
CSS做带三角形的效果
CSS做阴影效果
WoW Powerleveling
Posted on May 27, 2007 7:11 PM | | | Comments (0) | | TrackBacks (0)

引用地址(TRACKBACKS)
 
TrackBack URL for this entry:
http://www.wujianrong.com/mt-tb.cgi/5432

发布评论(ADD YOUR COMMENTS)
 
感谢您参与评论;发表您的意见时请保持文章的相关性;不相关的或是单纯宣传的内容可能会被删掉。您的E-mail只是用来确认您发表的文章,不会出现在网页上。
Please keep your comments relevant to this blog entry. Email addresses are never displayed, but they are required to confirm your comments.

称呼(Name):      记住我的个人信息(Remember)
邮箱(Email):
网址(URL):
评论(Add your comments):

相关内容
广告计划