0

因此,我试图根据单击的图标更改下一页的背景,但我不确定我的代码有什么问题!这只是一个测试页面(请原谅奇怪的代码,但我在实际网站本身中需要它!)

<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">

 body   {

background: url('<?php echo $_GET['image']; ?>';

}


</style>


<script>
    function showWeather(Weather) {
     //alert(Weather);
        myform.weather.value=Weather;
    //  alert(Weather);

    // ._Weather
        myform.submit();


}
</script>


<link href="teststyle.css" rel="stylesheet" type="text/css">
</head>
<body background="images/gradientsky.jpg"> 
<div id="weather">

<ul id="nav-reflection">
<form method="post" action="cloudy_name.php" id="myform">
    <li class="button-color-1"><a href="cloudy_name.php?image=images/cloudysky.png" onclick="showWeather('cloudy');" title="My fancy link"><img src="images/cloudybubble.png" width="211" height="180" align="left"></a></li> 

    <input type="hidden" name="weather" value="whatever">
</form>
</ul>
</div>

</body>
</html>
4

3 回答 3

1

您在这里有几个问题,我会尝试解决它们:

<form method="post" action="cloudy_name.php" id="myform">
        <li class="button-color-1"><a href="javascript:showWeather('cloudysky')" onclick="changeBgImage(images/cloudysky)" title="My fancy link"><img src="images/cloudybubble.png" width="211" height="180" align="left"></a></li>
</form>
  1. 您缺少“ul”包装器。列表项不能单独存在。
  2. 你说的是 showWeather('cloudy')。'cloudysky' 不是完整的文件名,所以可能是 cloudysky.jpg?
  3. 用户正在单击一个链接并被发送到另一个页面,因此一旦用户返回,您在此处使用 javascript 所做的任何更改都不会保留。
  4. document.getElementById(test) 需要引用一个字符串,比如 document.getElementById("test")

试一试并报告。

于 2012-04-20T19:58:10.017 回答
0

您的 changeBgImage 函数没有通过字符串 'image'

尝试这个:

    <script type="text/javascript">
        function changeBgImage (image) { // added Image
            var element = document.getElementById('test');
            element.style.backgroundImage = "url('"+image+"')"; // added single qoutes around image
            // Took out window.location - see comments below for explanation
            return false;// Added to stop Link execution
        }
    </script>
</head>
<body>
<div id="test">
    <form method="post" action="cloudy_name.php" id="myform">
        <li class="button-color-1"><a href="#" onclick="showWeather('cloudy'); changeBgImage('images/cloudysky.png');" title="My fancy link"><img src="images/cloudybubble.png" width="211" height="180" align="left"></a></li>
    </form>
</div>

我将所有 javascript 函数添加到 onclick 中,两者showWeatherchangeBgImage

请参阅下面的评论。

于 2012-04-20T20:07:41.893 回答
-2

IDEA:在链接上创建一个指向 php 文件的链接。类似 chg_bg.php?image=cloudy

在 php 文件上,创建一些代码来设置背景

<style>
    background: url('<php echo $_GET['image']; ?>';
</style>
于 2012-04-20T20:03:33.233 回答