將行爲移出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