<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">
http://www.w3.org/1999/xhtml">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>jquery Click Toggle and Change the icon src</title>
<script src= "Images/jquery-1.8.3.min.js" ></script>
<script>
function implementation
Parameter para1: ID of the element you want to hide
Parameter para2: ID of IMG that wants to change the image src
/*
function Toggle1 (PARA1,PARA2) {
if ($ ("#" +para2). attr ("src") = = "Images/col_24.jpg")
{
$ ("#" +para2). attr ("src", "images/col_11.jpg");
}
Else
{
$ ("#" +para2). attr ("src", "images/col_24.jpg");
}
$ ("#" +PARA1). Toggle ();
}
*/
jquery Closure Implementation
(function ($) {
$ (document). Ready (function () {
$ ("#div1"). Click (function () {
if ($ ("#img1"). attr ("src") = = "Images/col_24.jpg")
{
$ ("#img1"). attr ("src", "images/col_11.jpg");
}
Else
{
$ ("#img1"). attr ("src", "images/col_24.jpg");
}
$ ("#p1"). Toggle ();
});
$ ("#div2"). Click (function () {
if ($ ("#img2"). attr ("src") = = "Images/col_24.jpg")
{
$ ("#img2"). attr ("src", "images/col_11.jpg");
}
Else
{
$ ("#img2"). attr ("src", "images/col_24.jpg");
}
$ ("#p2"). Toggle ();
});
});
}) (JQuery);
</script>
<body>
<!--function to implement the HTML code section--
<!--
<div id= "Div1" style= "Width:350px;background-color: #0F3; height:30px; Vertical-align:middle; line-height:30px; " > Click here to hide the P1 below and change the icon src</div>
<p id= "P1" > P1</p> to Hide
<div id= "Div2" style= "Width:350px;background-color: #F00; height:30px; Vertical-align:middle; line-height:30px; " > Click here to hide the P2 below and change the icon src</div>
<p id= "P2" > P2</p> to Hide
-
<!--closures Implement the HTML code section--
<div id= "Div1" style= "Width:350px;background-color: #0F3; height:30px; Vertical-align:middle; line-height:30px; " > Click here to hide the P1 below and change the icon src</div>
<p id= "P1" > P1</p> to Hide
<div id= "Div2" style= "Width:350px;background-color: #F00; height:30px; Vertical-align:middle; line-height:30px; " > Click here to hide the P2 below and change the icon src</div>
<p id= "P2" > P2</p> to Hide
</body>
This article is from the "All the Way" blog, please be sure to keep this source http://cag2050.blog.51cto.com/1009647/1721873
jquery Click Toggle and change the image src