php dom html,PHP: DOM - Manual

This is a couple of classes to deal with yahoo yui menu.

/*

$menubar = new MenuBar();

$file = new Menu("File");

$file->setAttribute("href", "http://file.com");

$quit = new Menu("Quit");

$quit->setAttribute("href", "http://quit.com");

$file->appendChild($quit);

$menubar->appendChild($file);

echo $menubar->grab();

*/

//

// Author: Daniel Queirolo.

// LGPL

//

/** ---------------------------------

/**  Class MenuBar()

/**  Creates a the menubar and appends

/**  yuimenubaritems to it.

/** ---------------------------------*/

class MenuBar extends DOMDocument

{

public $menuID = "nav_menu";  // holds the css id that javascript yui menu code should have to recognize

private $UL;                  // This node holds every menu, This is THE node.

/** ---------------------------------

/**  Constructor

/**  Generates a menubar skeleton and the UL node

/** ---------------------------------*/

public function __construct() {

parent::__construct();

$rootdiv = parent::createElement("div");

$rootdiv->setAttribute("class", "yui-skin-sam");

parent::appendChild($rootdiv);

$yui_menubar = parent::createElement("div");

$yui_menubar->setAttribute("id", $this->menuID);

$yui_menubar->setAttribute("class", "yuimenubar");

$rootdiv->appendChild($yui_menubar);

$bd = parent::createElement("div");

$bd->setAttribute("class", "bd");

$yui_menubar->appendChild($bd);

$ul = parent::createElement("ul");

$ul->setAttribute("class", "first-of-type");

// ALL Menu() instances ocurr inside an

  • tag.

$this->UL = $bd->appendChild($ul);

}

/** ---------------------------------

/**  appendChild()

/**  Appends a new yuimenubaritem to the menubar UL node.

/**  This function changes

and classes to yuiMENUBARsomething

/** ---------------------------------*/

public function appendChild($child) {

$li = parent::importNode($child->LI, true);

$li->setAttribute("class", "yuimenubaritem");

$li->getElementsByTagName("a")->item(0)->setAttribute("class", "yuimenubaritemlabel");

$this->UL->appendChild($li);

}

public function grab() {

return parent::saveHTML();

}

}

/** ---------------------------------

/**  Class Menu()

/**  Creates a yuimenuitem li node

/** ---------------------------------*/

class Menu extends DOMDocument {

public $LI; // stores the

node (THE link) that will be exported to MenuBar() or used on appendChild()

/** ---------------------------------

/**  Constructor

/**  Generates a yuimenuitem li node

/**  No yuimenubar items are created here. MenuBar handles that.

/** ---------------------------------*/

public function __construct($link_name) {

parent::__construct();

$li = parent::createElement("li");

$li->setAttribute("class", "yuimenuitem");

// LI node stores THE link.

// if appendChild is used, the new (sub) Menu() would be LI node child.

$this->LI = parent::appendChild($li);

$a = parent::createElement("a", $link_name);

$a->setAttribute("class", "yuimenuitemlabel");

$li->appendChild($a);

$this->li = $li;

$this->a = $a;

}

/** ---------------------------------

/**  appendChild

/**  Appends a (sub) Menu() to current Menu() in LI

/** ---------------------------------*/

public function appendChild($child) {

$yuimenu = parent::createElement("div");

$yuimenu->setAttribute("class", "yuimenu");

$this->LI->appendChild($yuimenu);

$bd = parent::createElement("div");

$bd->setAttribute("class", "bd");

$yuimenu->appendChild($bd);

$ul = parent::createElement("ul");

$bd->appendChild($ul);

// child->NODE holds THE link from the new child (from child's __construct())

$ul->appendChild(parent::importNode($child->LI, true));

}

public function setAttribute($name, $value, $node="a") {

if ($node == "a") {

$this->a->setAttribute($name, $value);

}

else {

$this->li->setAttribute($name, $value);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值