mysql_fetch_array的div onmouseover仅适用于第一个结果

时间:2012-03-10 20:24:59

标签: php javascript css

我在这个网站上是新手,我希望你很快就会回答。

当用户将鼠标悬停在他们选择的朋友身上时,我希望显示删除图标,但它只出现在mysql_fetch_array的第一个结果中。对别人不起作用。你能帮我吗?我听说它已经完成了foreach功能,但不知道如何使用它,因为我是php的新手。

以下是代码。

PHP:

$q = mysql_query("SELECT * FROM `users`");

while ($row = mysql_fetch_array($q)) {

echo '<div id="friends" onmouseover="showRemove();" onmouseout="hideRemove();">';
echo '<div id="fillexit"></div>';
echo '<div id="showexit"></div>';
echo '</div>';

}

使用Javascript:

function showRemove() {
var l=document.getElementById("showexit");
var s=document.getElementById("fillexit");  
l.style.display = 'block';
s.style.display = 'none';
}


function hideRemove() {
var p=document.getElementById("showexit");
var o=document.getElementById("fillexit");  
p.style.display = 'none';
o.style.display = 'block';
}   

CSS:

#fillexit {
width:12px;
height:12px;
float:right;
}


#showexit {
width:12px;
height:12px;
background-color:#000000;
float:right;
text-align:center;
display:none;
}

我真的很高兴你的帮助。

2 个答案:

答案 0 :(得分:0)

       if($q ->num_rows()>0){

            foreach ($q ->result()as $row){

    echo '<div id="friends" onmouseover="showRemove();" onmouseout="hideRemove();">';
    echo '<div id="fillexit"></div>';
    echo '<div id="showexit"></div>';
    echo '</div>';
        }
        }

答案 1 :(得分:0)

这是因为从php脚本回显的html对所有行元素都有相同的id。

您应该使用计数器或其他东西创建一个唯一的ID,并相应地更改您的js。可能是你可以将id传递给js方法来帮助找到页面上的元素。

PHP

$q = mysql_query("SELECT * FROM `users`");
$count = 0;
while ($row = mysql_fetch_array($q)) {

echo '<div id="friends'.$count.'" onmouseover="showRemove('.$count.');" onmouseout="hideRemove('.$count.');">';
echo '<div id="fillexit'.$count.'"></div>';
echo '<div id="showexit'.$count.'"></div>';
echo '</div>';

$count = $count + 1;

}

JS

function showRemove(id) {
var l=document.getElementById("showexit" + id);
var s=document.getElementById("fillexit" + id);  
l.style.display = 'block';
s.style.display = 'none';
}


function hideRemove() {
var p=document.getElementById("showexit" + id);
var o=document.getElementById("fillexit" + id);  
p.style.display = 'none';
o.style.display = 'block';
}