ã¨ããããã®ç¬¬2åãã¼ããªããè¦åºããæãã£ããçãã§ããããæããã£ã¦ããã ãã
ããã§ãããæããã¨ã¯ãXMLããªã¼ã®æ§é ãè¦ãããè¦è¦åãããã¨ã§ããè¦è¦åã®å¯¾è±¡ã¨ãªãDOMããªã¼ã¯ããã¨ã®HTMLææ¸ã®ä¸é¨åã ã¨ãã¾ããããããã¦ãè¦è¦åã®æ段ã«ãã©ã¦ã¶ã®HTMLã¬ã³ããªã³ã°æ©è½ã使ãã¾ãã
ããããè¦è¦åãã§ããªãã¨ãããã°ã©ã ã®ãã¹ãï¼ãããã°ãããã«ããã®ã§ãã¾ãã¯ãæãããã¨ããã¯ããããã¨ã«ããã®ã§ãã
å 容ï¼
- DOMã®ç¥èã¯ããã ã
- å ¥ãåã®ç®±ã§è¡¨ç¾ãã
- ç®±ãä½ãé¢æ°ãä½ã
- è¦ç´ ã表ç¾ããç®±
- ç®±ãå®éã«æãé¢æ°
- 試ãã¦ã¿ã
- ä»åã®ã¾ã¨ã
âDOMã®ç¥èã¯ããã ã
XMLãDOMã®è©³ãã解説ã¯ãã¾ããããæå°éã®ãã¨ãã¾ã¨ãã¦ããã¾ããä»å使ãããããã£ï¼ã¡ã½ããã¯ä»¥ä¸ã®ãã®ã§ããåã®è¨è¿°ã«ã¯Java風ã®è¨æ³ã使ã£ã¦ãã¾ããï¼ä»åã®èª¬æã®é½åä¸ãNodeã¯ä½¿ãã代ããã«ElementOrTextã¨æ¸ãã¦ãã¾ããï¼
ååã¨å¼æ°å | å¤ã®å | 使ããã¯ã©ã¹ |
---|---|---|
createElement(String name) | Element | Document |
createTextNode(String data) | Text | Document |
appendChild(ElememtOrText child) | ElementOrText | Element |
setAttribute(String name, String value) | void | Element |
childNodes | ElementOrText[] | Element |
className | String | Element |
nodeType | Number | ElementOrText |
DOMã§ã¯ãããã¹ããã¼ã¿ã¯æååï¼stringï¼ã§ã¯ãªãã¦ãæååãå é¨ã«ä¿æããããã¹ããã¼ãï¼Textã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãããæ£ç¢ºã«ã¯Textã¤ã³ã¿ã¼ãã§ã¼ã¹ãæã¤ãªãã¸ã§ã¯ãï¼ã§è¡¨ç¾ããããã¨ã«æ³¨æãã¾ãããã
è¦ç´ ãã¼ããªãã¸ã§ã¯ãã®classNameããããã£ã¯ãHTMLã®classå±æ§ã¨åãæå³ã§ããããã©ã¦ã¶ã¯classå±æ§ããclassNameããããã£ã«åå¿ããããã§ããnodeTypeã¯ããã¼ãã®ç¨®é¡å¤å¥ã«ä½¿ãã¦ãå¤ã1ãªãè¦ç´ ãã¼ãã3ãªãããã¹ããã¼ãã§ãã
ãã以ä¸ã®è©³ãããã¨ã¯ä»ã®è³æã§èª¿ã¹ã¦ãã ããã
âå ¥ãåã®ç®±ã§è¡¨ç¾ãã
ããªã¼æ§é ã®è¦è¦åã«ã¯ãå ¥ãåã®ç®±ã使ãã¾ãã親ãã¼ããå¤ã®ç®±ãåãã¼ãã¯ãã®ä¸ã«è©°ãè¾¼ã¾ããç®±ã«ãªãã¾ãã
è¦ç´ ã®ç®±ï¼element boxï¼ã®ã¬ã¤ã¢ã¦ãã¯æ¬¡ã®ããã«ãã¾ãããã
ï¼---(element box)---ï¼
ï½ï¼---------------ï¼ï½
ï½ï½tag name ï½ï½
ï½ï¼---------------ï¼ï½
ï½ï¼---------------ï¼ï½
ï½ï½child #1 ï½ï½
ï½ï¼---------------ï¼ï½
ï½ï¼---------------ï¼ï½
ï½ï½child #2 ï½ï½
ï½ï¼---------------ï¼ï½
㻠⦠ã»
㻠⦠ã»
ï½ï¼---------------ï¼ï½
ï¼-------------------ï¼
ããã¹ãã®ç®±ã¯ãã£ã¨åç´ã§ã次ã®ããã§ãã
ï¼---(text box)----ï¼
ï½ text data ï½
ï¼-----------------ï¼
ãããã®ç®±ã¯ãHTMLã®divè¦ç´ ã§è¡¨ç¾ãã¾ãã
è¦ç´ ã®å±æ§ãããã®ä»ã®ãã¼ãï¼ã³ã¡ã³ããPIãCDATAã»ã¯ã·ã§ã³ãå®ä½åç §ï¼ã¯ãä»åã¯çç¥ãã¾ãã
âç®±ãä½ãé¢æ°ãä½ã
æ¹éã決ã¾ã£ãã®ã§ãå®è£ ãã¾ãããã以ä¸ã§ã¯ãå¹çãæªãã¦ãåé·ã§ããJavaScriptããã°ã©ã ãå°ããªé¢æ°ã«åå²ãã¦ã¡ãã£ã¨ãã¤ä½ã£ã¦ãããã¨ã«ãã¾ããããã§ã¯ãé¢æ°ã©ã¤ãã©ãªã»ã¹ã¿ã¤ã«ã§ä½ã£ã¦ããã¾ãããã¯ã©ã¹é¢¨ã«ã¾ã¨ããã³ã¼ãã¯å¥ã¨ã³ããªã¼ã§ç´¹ä»ãã¾ãã
ã¾ãã¯ç®±ï¼divè¦ç´ ï¼ãä½ãé¢æ°ï¼
function createBox(className) {
// className:string
// return:Elementvar box = document.createElement('div');
box.setAttribute('class', className);
box.className = className;
return box;
}
ç®±ã®ç¨®é¡ã¯classå±æ§ã«è¨é²ããã¾ããå±æ§classã¨ããããã£classNameããã¤ã§ãåæãã¦ããä¿è¨¼ããªãã®ã§ãæ示çã«ã両æ¹ãåãå¤ã«è¨å®ãã¦ãã¾ãã
ãã®createBoxã使ã£ã¦ãããã¹ããã¼ãã«å¯¾å¿ããç®±ãä½ãé¢æ°createTextBoxãå®ç¾©ãã¦ã¿ã¾ãã
function createTextBox(data) {
// data:string
// return:Element// ç´ æãæºå
var textBox = createBox('textBox');
var text = document.createTextNode(data);
// çµã¿ç«ã¦ã
textBox.appendChild(text);
// æ»ã
return textBox;
}
createTextBox("hello")ã¨å¼ã³åºãã¨ã次ã®ãããªHTMLè¦ç´ ãã§ãã¾ãã
<div class="textBox">hello</div>
classå±æ§å¤ã®'textBox'ãç®å®ã«ãCSSã«ããè¦ãç®ãå¶å¾¡ã§ãã¾ãããã¨ãã°ãç®±å ã®æåãè¶è²ãï¼maroonï¼å¤ªåã«ããããªãï¼
.texBox {color:maroon; font-weight:bold }
âè¦ç´ ã表ç¾ããç®±
è¦ç´ ã®ç®±ã®ã»ãã¯ããå°ãè¤éã«ãªãã¾ãã
function createElementBox(name) {
// name:string
// return:Element// ç´ æãæºå
var elementBox = elementBox = createBox('elementBox');
var tagNameBox = createBox('tagNameBox');
var tagNameText = document.createTextNode("<" + name + ">");
// çµã¿ç«ã¦ã
tagNameBox.appendChild(tagNameText);
elementBox.appendChild(tagNameBox);
// æ»ã
return elementBox;
}
createElementBox("foo")ã¨å¼ã³åºãã¨ã次ã®è¦ç´ ãè¿ãã¾ãã
<!-- ã¤ã³ãã³ãã¯è¦ãããã®ããã«ä»ãã -->
<div class="elementBox">
<div class="tagNameBox"><foo></div>
</div>
ãã®å®ç¾©ã§ãããã®ã§ãããç®±ã®è¡¨ç¤ºã®éã«ãæä¸ä½ã®è¦ç´ ã ãè¦ãç®ãå¤ãããã¨ããããã®ã§ã以ä¸ã®ããã«ç´ãã¦ããã¾ãã第2å¼æ°ãæå®ããã¦ãã¦trueã¨ã¿ãªããå¤ãªãã°ã種é¡ãrootBoxã§ããç®±ãè¿ã£ã¦ãã¾ãã
function createElementBox(name, isRoot_) {
// name:string
// isRoot_:any as boolean// ç´ æãæºå
var elementBox;
if (isRoot_) {
elementBox = createBox('rootBox');
} else {
elementBox = createBox('elementBox');
}
var tagNameBox = createBox('tagNameBox');
var tagNameText = document.createTextNode("<" + name + ">");
// çµã¿ç«ã¦ã
tagNameBox.appendChild(tagNameText);
elementBox.appendChild(tagNameBox);
// æ»ã
return elementBox;
}
âç®±ãå®éã«æãé¢æ°
次ã¯ç®±ãæãé¢æ°ã§ããã¾ãã¯ç°¡åãªãããã¹ãã®ç®±ãæãé¢æ°ï¼
function drawText(canvas, textNode) {
// canvas:Element
// textNode:Text
// return:voidvar box = createTextBox(textNode.data);
canvas.appendChild(box);
}
ããã§ãcanvasã£ã¦ã®ã¯ãç®±ãæãå ´æãæä¾ããHTMLè¦ç´ ã§ããcanvasãHTMLã®divã§ãããã¨ãï¼æé»ã«ï¼ä»®å®ãã¦ãã¾ããcanvasã«ç®±ï¼ãããdivè¦ç´ ï¼ãæ¿å ¥ããã¨ããã©ã¦ã¶ã®åã¬ã³ããªã³ã°ãèµ·ãã¦å®éã«æããããã¨ã«ãªãã¾ãã
次ã¯è¦ç´ ãæãé¢æ°ã§ãã第3å¼æ°ã«ããã«ã¼ããã©ãããæ¯ãåãã¾ãã
function drawElement(canvas, elmNode, isRoot_) {
// canvas:Element
// elmNode:Element
// isRoot_:any as boolean
// return:void// element node itself
var box = createElementBox(elmNode.tagName, isRoot_);
canvas.appendChild(box);
// child nodes
var children = elmNode.childNodes;
for (var i = 0; i < children.length; i++) {
switch(children[i].nodeType) {
case 1: // ELEMENT_NODE
drawElement(box, children[i]);
break;
case 3: // TEXT_NODE
drawText(box, children[i]);
break;
default:
// do nothing
break;
}
}
}
drawElementã®ãªãã§ãããä¸åº¦drawElementï¼ã¾ãã¯drawTextï¼ãå¼ãã§ã¾ãããããå帰å¦çã§ããã
â試ãã¦ã¿ã
ããã¾ã§ããã°ãå®éã«æããã¨ãã§ãã¾ãã次ã®ä¾ã®ãããªæ§é ãæãã¦ã¿ã¾ãã
<div id="target">
<h2>ãµã³ãã«</h2>
<p>ããã«ã¡ã¯ã</p>
<p>ããã¯<em>ãµã³ãã«</em>ã§ãã</p>
</div>
ä»ã¾ã§ã«ä½ã£ãé¢æ°ã1ã¤ã®ãã¡ã¤ã«drawxml-1.jsã«ã¾ã¨ããä¸ã§ããããã©ã¤ãã©ãªã¨ãã¦ä½¿ãã次ã®ã¹ã¯ãªãããå«ãã HTMLãã¡ã¤ã«ãä½ãã¾ãã
<script src="drawxml-1.js"></script>
<script>
function draw() {
var canvas = document.getElementById("canvas");
var target = document.getElementById("target");
drawElement(canvas, target, true);
}
</script>
ãã¨ã¯ãtextBox, rootBox, elementBox, tagNameBoxã¨ããHTML/CSSã¯ã©ã¹ã«å¯¾ãã¦ãé©å½ã«ã¹ã¿ã¤ã«ãå®ç¾©ãã¦è¦ãç®ã調æ´ãã¾ããdraw()ãå¼ã³åºããã¿ã³ãå¿ è¦ã§ããã
âä»åã®ã¾ã¨ã
ä»åã®è¦è¦åã§ã¯ãå±æ§ãªã©ãç¡è¦ãã¦ãããããã£ã³ãã¹ã®ã¯ãªã¢ãã§ããªããªã©æ©è½çã«ãä¸è¶³ãããã¾ããã§ããããããã¯å¿ è¦ã«ãªã£ããè£ããã¨ã«ãã¾ããã¨ãããããè¦ç´ ï¼ããã¹ãã®ããªã¼æ§é ãæããã¨ã¯ã§ããããã«ãªãã¾ããã
- æ°åã®DOM APIã¡ã½ããï¼ããããã£ã®ç¥èã§ãæããæä½ãã§ããã
- ããªã¼æ§é ã®è¦è¦åã«ã¯ãå ¥ãåã®ç®±ãæ軽ã§åãããããã
- ãç®±ãã¯ãclasså±æ§ãæå®ããdivè¦ç´ ã§å®ç¾ããã
- HTMLææ¸ããªã¼ã«ãç®±ããæ¿å ¥ããã ãã§ããã®ãç®±ããæããã
- ããã¨ã³ããªï¼å ¨ä½ç®æ¬¡ï¼
- 次å -