简体   繁体   English

HTML PHP onclick只工作一次

[英]HTML PHP onclick only working once

Okay, here we go. 好的,我们开始。 I have an image that is clickable (ingenious, right?): 我有一个可点击的图片(太难了,对吧?):

<DIV id="<?PHP echo($row[number]); ?>">                                                                                         
    <IMG TITLE="Favorite!" src="drool_bw_nobg.png" 
    align="center" onclick = "favorite(<?PHP echo($row[number]); ?>)"> &nbsp;
      <b>Like</b>
</DIV>

Keep in mind, I have already done this with PHP inline (example above), and done it with an echo() statement, and even a printf() statement with the \\'%s\\' wildcard. 请记住,我已经使用PHP内联(上面的示例)完成了此操作,并使用了echo()语句甚至使用了\\'%s\\'通配符的printf()语句来完成此操作。

Okay, when I click the image, the Favorite(id) javascript function is called. 好的,当我单击图像时,将调用Favorite(id) javascript函数。 The VERY first command is an alert(); 第一个非常命令是alert(); statement, followed by some AJAX code. 语句,后跟一些AJAX代码。

The latter works perfectly, and the php script that the AJAX references does its job very well when the image on the main page is clicked. 后者可以很好地工作,当单击主页上的图像时,AJAX引用的php脚本可以很好地完成其工作。

Then you can go onto click other images too, and the everything still works perfectly, but you click an image you have already clicked on...nothing...nada...zilch. 然后,您也可以单击其他图像,所有内容仍然可以正常使用,但是单击已单击的图像...什么都没有...纳达... zilch。 The alert() statement doesn't even pop up. alert()语句甚至不会弹出。

A real head scratcher. 真正的头刮刀。 Hopefully someone has had this problem before, and found a clever solution. 希望有人遇到过这个问题,并且找到了一个聪明的解决方案。

Here is my favorite() javascript function: 这是我最喜欢的()javascript函数:

function favorite(id){
  alert(id);
  getImage = document.getElementById(id);
  xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById(id).innerHTML=xmlhttp.responseText;
    }
  }

  xmlhttp.open("GET","getuser.php?img="+id,true);
  xmlhttp.send();
}

As I said, everything works the first time, but you have to refresh the page before you can click the same image again. 就像我说的,一切都在第一次运行,但是您必须刷新页面才能再次单击同一图像。

Well, here's the issue. 好吧,这就是问题。 You have this content (I've removed all irrelevant tags/attributes/PHP): 您拥有以下内容(我已删除了所有不相关的标签/属性/ PHP):

<DIV id="1">
    <IMG onclick="favorite(1);"/>
</DIV>

When you click on the IMG , the favorite() function gets called. 当您单击IMG ,会调用favorite()函数。 You then get your contents via AJAX, and update the innerHTML of the DIV element. 然后,您可以通过AJAX获取内容,并更新DIV元素的innerHTML At this point, you're replacing the DIV contents with the contents you retrieve from your AJAX request. 此时,您将用从AJAX请求中检索的内容替换DIV内容。

Most likely, the AJAX contents do not contain the onclick handler, so once you've updated the element once, your onclick handler is gone. 最有可能的是,AJAX内容不包含onclick处理程序,因此,一旦更新了元素一次, onclick处理程序就会消失。 Subsequent clicks will no longer call your favorite() method. 随后的点击将不再调用您的favorite()方法。

Just put the onclick handler on your DIV instead of your IMG , and everything will be fine. 只需将onclick处理程序放在DIV而不是IMG ,一切都会好起来的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM