Skip to content

spirit posteets tagged dom

 

  • 5 years ago
    1. function walkDom($node, $level = 0) {
    2.         $indent = ;
    3.         for ($i = 0; $i < $level; $i++)
    4.         $indent .= ‘&nbsp;&nbsp;&nbsp;&nbsp;’; //prettifying the output
    5.         if (true /*$node->nodeType == XML_TEXT_NODE*/) {
    6.                 echo $indent.‘<b>’.$node->nodeName.‘</b> – |’.$node->nodeValue.‘|’;
    7.                 if ( $node->nodeType == XML_ELEMENT_NODE ) {
    8.                         $attributes = $node->attributes; // get all the attributes(eg: id, class)
    9.                         foreach($attributes as $attribute) {
    10.                                 echo ‘, ‘.$attribute->name.‘=’.$attribute->value;
    11.                         }
    12.                 }
    13.                 echo ‘<br />’;
    14.         }
    15.         $cNodes = $node->childNodes;
    16.         if (count($cNodes) > 0) {
    17.                 $level++ ; // go one level deeper
    18.                 foreach($cNodes as $cNode)
    19.                         walkDom($cNode, $level);
    20.                 $level = $level1;
    21.         }
    22. }
  • 8 years ago
    1. $.create = function() {
    2.   var ret = [], a = arguments, i, e;
    3.   a = a[0].constructor == Array ? a[0] : a;
    4.   for(i=0; i<a.length; i++) {
    5.     if(a[i+1] && a[i+1].constructor == Object) { // item is element if attributes follow
    6.       e = document.createElement(a[i]);
    7.       $(e).attr(a[++i]); // apply attributes
    8.       if (a[i] == ‘table’) alert (‘titi’);
    9.       if(a[i+1] && a[i+1].constructor == Array) $(e).append($.create(a[++i])); // optional children
    10.       ret.push(e);
    11.     } else { // item is just a text node
    12.       ret.push(document.createTextNode(a[i]));
    13.     }
    14.   }
    15.   return ret;
    16. };
    17. // Usage
    18. $.create(
    19.    ‘table’, { ‘class’:“MyTable” }, [
    20.       ‘tbody’, {}, [
    21.          ‘tr’, { ‘class’:“MyTableRow” }, [
    22.             ‘td’, { ‘class’:“MyTableCol1” }, [ “howdy” ],
    23.             ‘td’, { ‘class’:“MyTableCol2” }, [
    24.                “Link:”, ‘a’, { ‘class’:“MyLink”, ‘href’:“http://www.example.com” }, [“example.com”] ] ],
    25.          ‘tr’, { ‘class’:“MyTableRow2” }, [
    26.             ‘td’, { ‘class’:“MyTableCol1” }, [ “howdy” ],
    27.             ‘td’, { ‘class’:“MyTableCol2” }, [
    28.                “Link:”, ‘a’, { ‘class’:“MyLink”, ‘href’:“http://www.example.com” }, [“example.com”] ] ] ] ]);

    “dom” related tags

    spirit’s tags