使用javascript单击图像后的页面链接

Page link after clicking image using javascript

本文关键字:链接 javascript 单击 图像 使用      更新时间:2023-09-26

所以我试图简单地根据网页上按下的图像重新定位页面。我以前有代码,但是对于我必须做的 100 件事来修复东西,它不再起作用了。谁能弄清楚为什么?

因此,当您单击现在页面上的图像cloudybubble.png时,它将带您进入 cloudy_name.php .并且还会有其他图像将您带到其他页面来执行此操作。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WhethertheWeather</title>
<!--<link rel="stylesheet" type="text/css" href="demo/css/screen.css" media="all" />-->
        <script src="http://www.adrianpelletier.com/mint/?js" type="text/javascript"></script>
        <script type="text/javascript" src="demo/scripts/jquery-1.3.2.min.js"></script>
        <script type="text/javascript" src="demo/scripts/jquery-ui-1.7.1.custom.min.js"></script>
        <script type="text/javascript" src="demo/scripts/execute.js"></script>

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

        }
    $(document).ready(function() {
        $('cloudybubble.png').click(function() {
            window.location="http://http://www.tcnjart.com/christineaustin/whethertheweather/cloudy_name.php;"
        });
    });
    // window.location="http://http://www.tcnjart.com/christineaustin/whethertheweather/cloudy_name.php"; //
</script>

</script>

<link href="style.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="javascript: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>

新脚本

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WhethertheWeather</title>
<!--<link rel="stylesheet" type="text/css" href="demo/css/screen.css" media="all" />-->
        <script src="http://www.adrianpelletier.com/mint/?js" type="text/javascript"></script>
        <script type="text/javascript" src="demo/scripts/jquery-1.3.2.min.js"></script>
        <script type="text/javascript" src="demo/scripts/jquery-ui-1.7.1.custom.min.js"></script>
        <script type="text/javascript" src="demo/scripts/execute.js"></script>

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

        }
    $('#cloudybubble').click(function() {
    window.location = "http://http://www.tcnjart.com/christineaustin/whethertheweather/cloudy_name.php;"
});
</script>

</script>

<link href="style.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="javascript:showWeather('cloudy')"; title="My fancy link"><img id="cloudybubble" src="images/cloudybubble.png" width="211" height="180" align="left" />
</a></li>
    <input type="hidden" name="weather" value="whatever">
</form>
</ul>
</div>
</body>
</html>

编辑:修改以考虑评论中的新信息。

我们可以为您的页面中的所有图像通用地工作,以便它们使用相同的JavaScript。

在 HTML 中,将超链接放回 a 标记中。然后,在 a 标签中添加一个类,我建议"weatherLink"。然后将您要登录的文本放入数据库中,即"多云",并将其作为链接的 id。

.HTML:

<li class="button-color-1">
    <a id="cloudy" class="weatherLink" href="http://www.tcnjart.com/christineaustin/whethertheweather/cloudy_name.php" title="My fancy link">
        <img src="images/cloudybubble.png" width="211" height="180" align="left" />
    </a>
</li>

现在我们的 JavaScript 需要更新 jQuery 点击处理程序,如下所示。

JavaScript:

$(document).ready(function () {
    $('a.weatherLink').click(function (e) {
        e.preventDefault();
        showWeather(this.id);
        window.location.href = this.href;
        return false;
    });
});

现在,如果您添加任何其他图像,只需将超链接保留在 a 标签中,为其提供 weatherLink 类,并为其提供一个可以传递给 showWeather() 的唯一 ID。给定正确的 HTML,您不需要再复制任何 JavaScript。

这是一个jsFiddle测试页面,供您查看结果:http://jsfiddle.net/willslab/jkB9z/9/

你绝对应该在服务器端这样做。

如果你的用户没有javascript怎么办?相信我,其中有很多(主要是出于安全原因)。

将表单的操作定义为某个 PHP 页面,处理您需要执行的更改或 w/e,并相应地重定向用户(使用 header('Location: http://www.hi.com'); )。

id添加到img标签中,并在选择器中使用它。