2017-09-12 52 views
1

下面你会发现我的代码用于从表格中提取数据以及我想用最后一个按钮标签中列出的每个表格行重复的图像Class = plusbtn重复元素的onclick事件只适用于第一行

{ 

     $event .= '  <div style="border-bottom:1px solid gray;padding:2px;width:100%;float:left;"><div id="eventslist" onclick="goevent('.$row['id'].');"> 
         <div style="width:100%;float:left;"> 
          <h4 style="margin:0;"><span id="eventuser" style="width:50%;float:left;text-align:left;font-size:14px;"><img src="https:///login/profile/'.$row['profile_img1'].'" style="width:35px;height:37px;border-radius:20px;margin-left:-4%;background:url(http:///img/person-placeholder.jpg) no-repeat center" class="img-rounded"> <b>'.$row['user'].'</b></span><span style="float:right;text-align:right;font-size:12px;margin-top:9px;margin-right:-25px;">Posted: '.$row['stdate'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><br/><br/><b><span style="font-size:20px;float:left;">'.$row['fname'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></b></h4> 
         </div> 
         <div style="width:109%;float:left;"> 
          <img src="http:///login/image/'.$row['name'].'" style="width:100%;height:35%;border-radius:10px;margin-left:-4%;background:url(https:///img/load.gif) no-repeat white" class="img-rounded"> 
         </div> 
         <div style="width:100%;float:left;"> 
          <p style="margin-bottom:0;">'.substr($row['description'],0,110).'...</p> 
         </div> 
       </div><div><button class="plusbtn" onclick="plusrate(\''.$likecount.'\');"><a onclick="document.getElementById(`myImage`).src=`https:///img/fav4.png`"><img id="myImage" src="https:///img/fav3.png" style="opacity: 0.7;height:25px;max-width:100px"></a></button><span id="likeqnty" class="likeqnty">0</span> <b>Likes</b></div></div> '; 
    } 

使用该设置按钮图像出现的每个条目,但我只能点击按钮在我的网页上的第一项。当我在页面上出现按钮(回声“按钮”)时输入回显

如何使每个表格条目重复一次,并正确地将fav4转换为fav3。

回答

0

你的问题在于ID应该是唯一的。但是所有的图像都有相同的ID。

<a onclick="document.getElementById(`myImage`).src=`https:///img/fav4.png`"> 
    <img id="myImage" src="https:///img/fav3.png" style="opacity:0.7;height:25px;max-width:100px"> 
</a> 

点击任何这些链接将始终与ID myImage改变第一张图像。

您想更改您的onclick javascript以将图像分别定位到当前点击目标。

<a onclick="this.getElementsByTagName(`img`)[0].src=`https:///img/fav4.png`"> 
    <img id="myImage" src="https:///img/fav3.png" style="opacity:0.7;height:25px;max-width:100px"> 
</a> 

在这段代码中,我们使用它,因为它是当前接收到点击的DOM元素。并检索其中的图像,并更改其src值。这确保了目标图像与用户点击的位置相关。

+0

由于点击时img/fav4切换到img/fav3,所以ID相同。这是一个像按钮,第一个图像是一个开放的心,第二个图像被点击时是一个填充的心脏 – Answerme

+0

我认为你在混合ID和SRC ... SRC是图片SouRCe,而ID只是一个标识符html元素。 – Salketer

相关问题