<?xml version="1.0" encoding="ISO-8859-1"?>
<agents>
<agent>
<image> img/sara.jpg</image>
<id>1</id>
<name>Tommy Jenkin</name>
<company>CJenkins Insurance</company>
<street>Insurance150 S State Stree</street>
<city>Linkend</city>
<phone>(773) 561-4331</phone>
</agent>
<agent>
<image> img/primary-nav-logo.png</image>
<id>2</id>
<name>saka</name>
<company>LIC Insurance</company>
<street>Insurance150 S State Stree</street>
<city>Linkend</city>
<phone>(773) 561-4331</phone>
</agent>
</agents>
我正在通过xml.php文件显示这些数据 我的xml.php文件中的代码如下所示
$xml = simplexml_load_file("agent.xml") or die("Error: Cannot create object");
foreach($xml as $agent){
function processXML($node){
foreach($node->children() as $agent => $data){
$agent= trim($agent);
if($agent!="" && $agent=='id')
{
echo '<div class = "inline">';
echo '<input type = "button" name="Agent" id = "'.$data.'" class = "subs-btn" value = "Select this Agent" OnClick = Selected(this.id);>';
echo '</div>';
echo '<div>';
}
elseif($agent!="" && $agent=='image')
{
echo '<div class = "inline"><img src="'.$data.'" ></div>';
echo '<div>';
echo '</div>';
}
elseif($agent!="")
{
echo '<div class = "inline">';
echo $data;
echo '</div>';
echo '<br/>';
}
processXML($data);
}
}
processXML($xml);
}
现在在每个要显示的代理我正在生成带有以下代码的按钮
echo '<input type = "button" name="Agent" id = "'.$data.'" class = "subs-btn" value = "Select this Agent" OnClick = Selected(this.id);>';
在这个按钮中,我调用了javascript的功能,下面通过该代码给出了我正在改变按钮的背景图像。
function Selected(elem)
{
alert(elem);
document.getElementById(elem).style.backgroundImage="url('images/subs-btn-act.png')";
unselected();
}
现在我很困惑如何将所有其他按钮的图像更改为默认图像。我的意思是如果我点击一个按钮,那么该特定按钮的背景图像应该改变。然后,如果我点击另一个按钮,则第一个单击的按钮应该将背景图像更改为默认值。
请建议我如何做到这一点。
答案 0 :(得分:0)
记住之前选择的按钮,并在选择新按钮时将其bg-image更改回默认值。 通过清除指定的bg-image更改回默认值:
function Selected(elem)
{
if(previous) { document.getElementById(previous).style.backgroundImage=""; }
document.getElementById(elem).style.backgroundImage="url('images/subs-btn-act.png')";
previous = elem; // remember current selection
unselected();
}