将行为移出HTML并放入<script>
标记或更好的外部脚本文件(尽管可以将事件附加到HTML元素)是最佳做法。在这种情况下,没有理由将图像元素绑定到与图像无关的代码(它看起来像试图填充其他元素)。
<script>
// This code will not work with older IE versions; you may
// wish to use a library like jQuery to handle all the complexities
// of browser compatibility; or you can avoid all this by putting
// the script tag after your elements
window.addEventListener('DOMContentLoaded', function() {
var rand = open_boosters();
document.getElementById("?").innerHTML=rand;
document.getElementById("randImg").src = "site3.php?randomNumber"+rand;
}, false);
</script>
<img id="randImg" />
然后在你的PHP代码:
<?php
$_GET['randomNumber']; // Do something with it (though safely--e.g., do not add directly to a SQL string, etc.)
?>
它看起来像你的HTML是无效的。你正在尝试去site3.php?action = someid,但是你有一个单独的动作属性。你可以发布一个你到目前为止的jsfiddle(当然不包括php)吗? – james31rock 2012-07-31 00:22:05