以下HTML/JavaScript代碼將背景圖像插入到div中,而不會導致問題。如何在PHP中運行JavaScript代碼
<!DOCTYPE html>
<html lang = "en">
<head>
<title>to-forum.html</title>
<script>
function ChangeImage(param)
{
return("http://localhost/images/greensilver.gif");
}
</script>
</head>
<body>
<div id="logo" style = "height:400px; width:400px";>
<script>
var xyz = ChangeImage();
document.getElementById("logo").style.backgroundImage = "url('" + xyz + "')";
</script>
</div>
</body>
</html>
我需要運行從PHP代碼js腳本所以我寫了:
<!DOCTYPE html>
<html lang = "en">
<head>
<script>
function ChangeImage(param)
{
return("http://localhost/tlushim/images/greensilver.gif");
}
</script>
</head>
<body>
<div id="logo" style = "height:400px; width:400px";>
<?php
echo '<script>',
'var xyz = ChangeImage"('" + 1 + "')";',
'document.getElementById"('" + logo +"')".style.backgroundImage = "url('" + xyz + "')";',
'</script>'
?>
</div>
</body>
</html>
隨着第二碼我不走運/這一次所需的背景圖像沒有露面,可能,因爲 我不知道如何從PHP運行JS代碼。 任何人都可以幫助我嗎? 謝謝
您無法在PHP文件中運行JS代碼。 PHP在服務器上運行,JS在客戶端的瀏覽器上運行。難道你不能只爲你的logo元素添加一個'style =「background-image:url(...); height:...」'? – PeterMader
謝謝。我不再從PHP運行JS。這是個錯誤 ! –