Having trouble assigning hyperlinks to .gifs that become visible through .toggle
Hi everyone, new to jquery and the forum and even quite inexperienced with html.
So, I have this code:
- <head>
- <title>Title</title>
- <meta http-equiv="Content-Type" content="text /html; charset=UTF-8" />
- <link rel="stylesheet" type="text/css" href="mystyle.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $('p a img').each(function(){var path = $(this).attr('src').slice(0,-4);
- $(this).toggle(function(){
- $(this).attr('src',path + '-hover.gif');
- $('p.link').html('<img src="' + path + '-text.gif" alt="" />');},
- function(){
- $(this).attr('src',path + '.gif');
- $('p.link').html('<img src="_img/buttons/dummy.gif" alt="" />');});
- });
- });
- </script>
- </head>
- <body>
- <h1>Title</h1>
- <p>
- <a href="#"><img src="_img/buttons/home.gif" alt="Home" /></a>
- <a><img src="_img/buttons/mail.gif" alt="Mail" /></a>
- </p>
- <p class="link">
- <img src="_img/buttons/dummy.gif" alt="" />
- </p>
- </body>
My problem: I want to make the "mail-text.gif"s that becomes visible through clicking on the "mail.gif" a hyperlink and also have the option to add further ".gif"s and make the corresponding "-text.gif"s hyperlinks.
As long as there is only the "mail.gif" and no "home.gif" or other, I can solve this by changing
- $('p.link').html('<img src="' + path + '-text.gif" alt="" />');
into
- $('p.link').html('<a href="mailto:mail@mail.com"><img src="' + path + '-text.gif" alt="" />');
or
- $('p.link').html('<a href="mailto:mail@mail.com"><img src="_img/buttons/mail-text.gif" alt="" />');
From here on, I do not have much of a clue what to try next.
I have two other questions that are currently low priority and thus optional:
- b. What does the .slice(0,-4) do in this case?
- c. Is there an easy way do add a hover to the 'p a img' that would work on a pc and not screw up the rest on a touch device?
Thank you very much!