我有一个PHP代码,用于填充数据库中的表。 l使用此代码
将表中的所有数据回显到表中foreach ($m as $value=>$key){
echo "<tr><td onmouseover = 'show()' onmouseout='hide()' >" . $key['name'] . "<a href='add.php' id='editlink'> Edit</a>" . "</td><td>" . $key['office_name'] . "</td><td>" . $key['description'] . "</td><td>" . $key['image'] . "</td></tr>";
}
应用于editlink
<script>
function show()
{
document.getElementById("editlink").style.display = "inline";
}
function hide()
{
document.getElementById("editlink").style.display = "none";
}
</script>
它工作正常,但编辑链接仅显示表格中的第一行。如何在每一行中显示链接。谢谢
答案 0 :(得分:0)
您需要区分行并将PHP修改为以下内容:
<?php
$i = 1;
foreach ($m as $value=>$key) {
echo "<tr onmouseover = 'show(".$i.")' onmouseout='hide(".$i.")'><td>" . $key['name'] . "<a href='add.php' id='editlink".$i."'> Edit</a>" . "</td><td>" . $key['office_name'] . "</td><td>" . $key['description'] . "</td><td>" . $key['image'] . "</td></tr>";
$i++;
}
JavaScript应该是这样的:
<script>
function show(i) {
document.getElementById("editlink"+i).style.display = "inline";
}
function hide(i) {
document.getElementById("editlink"+i).style.display = "none";
}
</script>
答案 1 :(得分:0)
我找到了办法。 l使用document.getELementsByClassName返回所有tr的节点列表然后循环遍历列表以插入编辑链接..代码将更好地解释
<?php
$m = edit_keymember();
if(!(is_array($m))){
echo "not array";
}
foreach ($m as $value=>$key){
echo "<tr><td onmouseover = 'show()' onmouseout='hide()' >" . $key['name'] . "<a href='add.php' class='editlink' > Edit</a>" . "</td><td>" . $key['office_name'] . "</td><td>" . $key['description'] . "</td><td>" . $key['image'] . "</td></tr>";
}
?>
Javascript
<script>
function show() {
var i;
var len = document.getElementsByClassName("editlink");
for ( i = 0 ; i <= len.length ; i++){
len[i].style.display = "inline";
}
}
function hide(){
var i;
var len = document.getElementsByClassName("editlink");
for ( i = 0 ; i <= len.length ; i++){
len[i].style.display = "none";
}
}
</script>
我是一名新手开发者,我对所有建议表示感谢,并会尽快提出更多信息......很棒......谢谢
答案 2 :(得分:-1)
我认为这样可以正常工作:
<?php
foreach ($m as $key => $value) { ?>
<tr>
<td onmouseover = 'show()' onmouseout='hide()'> <?php $key['name'] ?> <a href='add.php' id='editlink'> Edit</a> </td>
<td> <?php $key['office_name'] ?> </td>
<td> <?php $key['description'] ?> </td>
<td> <?php $key['image'] ?> </td>
</tr>
<?php } ?>