- 用JS的DOM方法去处理一段HTML内容,从HTML结构中取得一些东西然后添加一些东西
explanation.html:
Explaining the Document Object Model What is the Document Object Model?
The W3C defines the DOM as:
A platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents.
It is an API that can be used to navigate HTML and XML documents.
displayAbbreviation.js:
function displayAbbreviations() { if (!document.getElementsByTagName || !document.createElement || !document.createTextNode) return false;// get all the abbreviations var abbreviations = document.getElementsByTagName("abbr"); if (abbreviations.length < 1) return false; var defs = new Array();// loop through the abbreviations for (var i=0; i
addLoadEvent.js:
function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } }}
注意这个要注意点,在下一篇里面会讲到,一个兼容性的Bug
我用的Chrome,其实现在的现代浏览器用这本书的这个例子代码就会有问题了,如果多个JS。下一篇讲。