编写html与js交互网页心得:编写两个按钮切换显示不一样的图片

第一步:先创建一个html网页,以下:html

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
 </head>
 <body>
  <div>
   <div>
        <input class="btnhy1" type="button" value="一楼" style="width: 80px;height: 30px; border: 2px; background: #2290E0;"/>
       <input class="btnhy2" type="button" value="二楼" style="width: 80px;height: 30px; border: 2px; background: #2290E0;"/>
     </div>
    <div>
        <img id="image1" src="img/111.jpg"/>
      <img id="image2" src="img/222.jpg"/>
    </div>
  </div>
  
  <script src="./js/jquery-1.8.3.min.js"></script>
  <script src="./js/index.js"></script>
 </body>
</html>jquery

须要注意的是:index.js使用“$”时,必需要引用“ <script src="./js/jquery-1.8.3.min.js"></script>”,要否则就会报 “Uncaught ReferenceError: $ is not defined”错误,若是引用的路径不对就会报:“Failed to load resource: net::ERR_FILE_NOT_FOUND”ide

第二步:编写index.js的代码,代码以下:htm

$(".btnhy1").click(function() {
 //alert("111")
 $("#image1").show();
 $("#image2").hide();
});
$(".btnhy2").click(function() {
 //alert("222")
 $("#image2").show();
 $("#image1").hide();
});
 
总结:class  使用的是.(dot),id使用的是#。
相关文章
相关标签/搜索