Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在DOM中分配事件处理程序_Php_Html_Dom - Fatal编程技术网

Php 在DOM中分配事件处理程序

Php 在DOM中分配事件处理程序,php,html,dom,Php,Html,Dom,我正在用DOM在PHP中动态创建一个元素。 我成功地创建了元素,创建了属性,但是我想知道我是否可以在这里分配事件处理程序 例如,我想将onmouseover=“function”分配给元素。那么有什么方法可以做到这一点吗 $dom = new DOMDocument('1.0'); //Create new document with specified version number $a = $dom->createElement('a',$row['sname']); //Creat

我正在用DOM在PHP中动态创建一个元素。 我成功地创建了元素,创建了属性,但是我想知道我是否可以在这里分配事件处理程序

例如,我想将
onmouseover=“function”
分配给元素。那么有什么方法可以做到这一点吗

$dom = new DOMDocument('1.0'); //Create new document with specified version number

$a = $dom->createElement('a',$row['sname']); //Create new a tag
$dom->appendChild($a); //Add the a tag to document

$class = $dom->createAttribute('class'); //Create the new attribute 'type'
$class->value = 'button';
$a->appendChild($class);

$href = $dom->createAttribute('href'); //Create the new attribute 'type'
$href->value = '#';
$a->appendChild($href);

为什么不像添加class属性一样添加事件呢?本质上,
onmouseover
是另一个属性

$dom = new DOMDocument('1.0'); //Create new document with specified version number

$a = $dom->createElement('a',$row['sname']); //Create new a tag
$dom->appendChild($a); //Add the a tag to document

$class = $dom->createAttribute('class'); //Create the new attribute 'type'
$class->value = 'button';
$a->appendChild($class);

$event = $dom->createAttribute('onmouseover'); //Create the new attribute 'type'
$event->value = 'my function';
$a->appendChild($event);

$href = $dom->createAttribute('href'); //Create the new attribute 'type'
$href->value = '#';
$a->appendChild($href);

输出:

<a class="button" onmouseover="my function" href="#">test</a>