Supercharging our example website

Now we‘ve gone over a few JavaScript basics, let‘s add a few cool basic features to our example site to give you some first idea of what is possible.

Adding an image changer

In this section we‘ll add another image to our site, and add some simple JavaScript to change between the two when the image is clicked on.

  1. First of all, find another image that you‘d like to feature on your site. Make sure it is the same size as your first image, or as close as possible.
  2. Save the image in your images folder.
  3. Go to your main.js file, and enter the following JavaScript (if your hello world JavaScript is still there, delete it):

    var myImage = document.querySelector(‘img‘);
    
    myImage.onclick = function() {
        var mySrc = myImage.getAttribute(‘src‘);
        if(mySrc === ‘images/firefox-icon.png‘) {
          myImage.setAttribute (‘src‘,‘images/firefox2.png‘);
        } else {
          myImage.setAttribute (‘src‘,‘images/firefox-icon.png‘);
        }
    }
    

      

  4. Save all files and load index.html in the browser. Now when you click the image, it should change to the other one!

So here, we are storing a reference to our image element in the myImage variable. Next, we make this variable‘s onclick event handler property equal to an anonymous function. Now, every time this image element is clicked:

  1. We retrieve the value of the image‘s src attribute.
  2. We use a conditional to check whether the src value is equal to the path to the original image:
    1. If it is, we change the src value to the path to the 2nd image, forcing the other image to be loaded inside the <image> element.
    2. If it isn‘t (meaning it must already have changed), we change the src value back to the original image path, to flip it back to how it was originally.
时间: 2024-08-06 11:53:12

Supercharging our example website的相关文章

Permanent Link: 101 ways to speed up your Magento e-commerce website

101 ways to speed up your Magento e-commerce website May 18, 2010/in E-commerce, Magento /by Guido Jansen As you probably know by now, Google is Using site speed in web search ranking. And I couldn’t agree more: speed is important for your site, and

配置WebSite的IIS时遇到的问题与解决方法

1: Server Error Internet Information Services 7.5 Error Summary HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid. Detailed Error Information Module IIS Web

远程调试 Azure 上的 Website

让我们先检查一下使用的 Azure SDK 版本和 Visual Studio 版本.根据MSDN的介绍,Azure 的远程调试功能是在 Azure SDK 2.2 中加入的,所以请确保您的机器上安装了2.2或者是更新的 Azure SDK. 关于 Visual Studio 的版本问题,MSDN上的描述也很有趣.大意是使用 Visual Studio 2012 已经可以远程调试 Azure 上的 Website 了,但是 Visual Studio 2013 又对此做了很多的提升.个人理解最好

(转)配置Website的IIS时遇到的问题与解决方法

在部署WebSite时遇到问题,刚好发现Eric Sun的文章,因此转载做个副本. 原文地址:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/11/04/2235630.html 1: Server Error Internet Information Services 7.5 Error Summary HTTP Error 500.19 - Internal Server Error The requested page canno

Azure Website - UTC Time and Local Time

Key: Azure Website uses UTC time on the server How to: show local time in website UI? Solution: #1 Use TimeZoneInfo to convert UTC to local time var cetZone = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");         var local

Talk In Web Security(安全世界观): Devleping a Secure WebSite

Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. Why to write about Web Security? A java file can hack your server.One JSP can download any file. How to do this?  1. Write a JSP and upload to the server.  2. Use JSP to dow

Cannot attach the file &#39;E:\TeamWork\Fido\Fido.WebSite\App_Data\FidoData.mdf&#39; as database &#39;FidoData&#39;.

打开GuGet命令框,输入如下命令就可以了 PM> sqllocaldb.exe stop v11.0 LocalDB instance "v11.0" stopped. PM> PM> sqllocaldb.exe delete v11.0 LocalDB instance "v11.0" deleted. PM> sqllocaldb.exe start v11.0 LocalDB instance "v11.0" s

一些参考网站 - Reference Documentation - Website Address

Reference Documentation - Website Address MSDN Visual Studio 2015官方文档 https://msdn.microsoft.com/zh-CN/library/sc65sadd.aspx 提供visual stuio开发过程中的帮助文档 Microsoft Technet技术中心 https://technet.microsoft.com/zh-cn/ MySQL官方安装器 http://dev.mysql.com/downloads

the book for communication or sharing the gratitude and the video website

The four things that matter most https://storycorps., which is the website devoted to recording the moment in your life.