在下面的代码中,
$item->setAttribute('onclick','target_popupAFB(this)');这让我很头疼,它写了值target_popupAFB(this),但没有写名字。结果是="target_popupAFB(this)“
我是一个使用DOM的新手
$dom = new DOMDocument();
$dom->loadHTML($doc);
$dom->preserveWhiteSpace = false;
$links = $dom->getElementsByTagName('a');
foreach ($links as $item)
{
$tempsrc=$item->getAttribute("href");
if(substr($tempsrc,-4)==".mp3")
{
$item->removeAttribute('target');
$item->setAttribute('target','msp');
}
$tempta=$item->getAttribute("target");
if($tempta!=="msp")
{
$item->removeAttribute('target');
$item->setAttribute('target','_blank');
}
if(substr($tempsrc,-4) ==".jpg" || substr($tempsrc,-4) ==".bmp" || substr($tempsrc,-4) ==".gif" || substr($tempsrc,-4) == ".png")
{
$imgA=explode("/",$tempsrc);
$c=count($imgA);
if($imgA[1]=="AFBEELDINGEN" && $imgA[0]=="..")
{
$img=end($imgA);
$item->setAttribute('href','http://'.$_SERVER['SERVER_NAME'].'/AFBEELDINGEN/afb.php?img=' .$img);
$item->setAttribute('onclick','target_popupAFB(this)');
}
}
}
$doc = $dom->saveHTML();发布于 2014-04-23 21:27:41
这不是主要的事情: thinqs to https://developer.mozilla.org/fr/docs/DOM/element.setAttribute,你不需要移除属性并设置一个新的属性。setAttribute将覆盖该属性,但如果存在的话。
我认为当你想要添加事件时,不应该使用setAttribut,而应该使用addEventListener (文档:https://developer.mozilla.org/fr/docs/DOM/element.addEventListener)。很高兴它能帮上忙。总帐
https://stackoverflow.com/questions/23245187
复制相似问题