2017-05-24 7 views
2

でノードを作成し、複数の属性とそれを設定します。これは私がやろうとしていたいさ</p> <p>を私はノードのスクリプトを作成したい複数の属性

$script_insta_node = $dom->createElement('script'); 
$script_insta_node->setAttribute('async defer src', $instagram_js_path); 

問題は、私は一つだけを定義することができるということです属性。

+2

変数を呼び出し、yそれが必要ですか? – Pyromonk

答えて

0

あなたができることは、DOMElement(属性ミューテータメソッドを定義するクラス)を拡張して、DOMDocumentで登録してください。

もう少し気楽にしたい場合は、DOMDocumentを拡張して、デフォルトでこの動作を組み込むことができます。たとえば、自分のネームスペースDom;

ドム\要素

namespace Dom; 

class Element 
    extends \DOMElement 
{ 
    // I think something like this would make 
    // a little more sense than your original example 
    // that would probably set all attributes to the same value 
    public function setAttributes(array $attributes) { 
    foreach($attributes as $name => $value) { 
     $this->setAttribute($name, $value); 
    } 
    } 
} 

ドム\ドキュメント

namespace Dom; 

class Document 
    extends \DOMDocument 
{ 
    public function __construct($version = '1.0', $encoding = null) { 
    parent::__construct($version, $encoding); 

    // we register our custom DOMElement class 
    // you can do this for all DOM classes 
    // that are derivatives of DOMNode 
    $this->registerNodeClass('DOMElement', 'Dom\Element'); 
    } 
} 

使用例:取り込んでカスタム関数を定義することができなく何

$doc = new Dom\Document; 
$scriptElement = $doc->createElement('script'); 
$scriptElement->setAttributes(array(
    'defer' => '', 
    'async' => '', 
    'src' => $instagram_js_path 
)); 
関連する問題

 関連する問題