Enhacing the content with JavaScript
2015-09-14 15:18
453 查看
What not to do :
In theory , you could use JavaScript to add important content to a web page.
However, that would be a very bad idea, because there would be no room for graceful degradation.
Visitors lacking the necessary JavaScript support would never see the content.
The markup : HTML, XHTML, or HTML5
For the markup, its up to you whether to use HTML or XHML.
The important thing is that whichever document type you choose, the markup validates to the
specified DOCTYPE declaration.
With XHTML, all the tags must be closed. That includes stand-alone elements like <img> and <br>
which must be written with a closing slash: <img /> and <br />
To use the XHTML DOCTYPE, you include this at the begining of your document :
Another option that you may like even better is to use the HTML5 DOCTYPE. The HTML5 DOCTYPE is
very simple (just 15 characters) :
You can create useful scripts like these using just a handful of DOM methods and porperties.
Having well-structured markup is an important prerequisite when you are enhancing content
with DOM scripts.
As a recap, these are the most useful methods for retrieving information from a document :
getElementById
getElementsByTagName
getElementsByName
getAttribute
These are the most useful methods for attaching information to a document :
createElement
createTextNode
appendChild
insertBefore
setAttribute
By combining those methods, you can create very powerful DOM scripts.
Always remember to use JavaScript to enhance the content of your documents, rahter than
creating any core content directly with the DOM.
In theory , you could use JavaScript to add important content to a web page.
However, that would be a very bad idea, because there would be no room for graceful degradation.
Visitors lacking the necessary JavaScript support would never see the content.
The markup : HTML, XHTML, or HTML5
For the markup, its up to you whether to use HTML or XHML.
The important thing is that whichever document type you choose, the markup validates to the
specified DOCTYPE declaration.
With XHTML, all the tags must be closed. That includes stand-alone elements like <img> and <br>
which must be written with a closing slash: <img /> and <br />
To use the XHTML DOCTYPE, you include this at the begining of your document :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strick.dtd" >
Another option that you may like even better is to use the HTML5 DOCTYPE. The HTML5 DOCTYPE is
very simple (just 15 characters) :
<!DOCTYPE html>
You can create useful scripts like these using just a handful of DOM methods and porperties.
Having well-structured markup is an important prerequisite when you are enhancing content
with DOM scripts.
As a recap, these are the most useful methods for retrieving information from a document :
getElementById
getElementsByTagName
getElementsByName
getAttribute
These are the most useful methods for attaching information to a document :
createElement
createTextNode
appendChild
insertBefore
setAttribute
By combining those methods, you can create very powerful DOM scripts.
Always remember to use JavaScript to enhance the content of your documents, rahter than
creating any core content directly with the DOM.
相关文章推荐
- JavaScript新增输入框
- JS原生编写飞机大战小蜜蜂游戏
- JS+DIV+CSS实现的经典标签切换效果代码
- JS----简单的倒计时
- json 除去转义字符以及查看json错误
- JSHint 使用说明
- extjs 4.2 按照百分比配置
- JSON 解析器的简单例子
- Javascript动态创建 style 节点
- 淘宝轮播JS
- javascript根据输入的本金实时计算本息和
- JavaScript运算符
- 更深入学习js的一些细节记录(看到哪里先记录到哪)
- JavaScript 库
- 【javascript】arrayBuffer对象的理解
- JS正则表达式验证数字非常全
- 使用 JavaScript 创建Cookie
- AJAX获取json
- js提取整数部分,移除首末空格
- javascript 与 flex 通信