jQuery通过设置元素的属性实现图片随机切换
jQuery通过设置元素的属性实现图片随机切换
jQuery通过设置元素的属性实现图片随机切换:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>设置元素的属性(二)</title>
- <script type="text/javascript" src="../Jscript/jquery-1.4.2.js">
- </script>
- <script type="text/javascript" src="../Jscript/jquery-1.4.2-vsdoc.js">
- </script>
- <style type="text/css">
- body {
- font-size: 12px
- }
- .clsSpn {
- float: left;
- padding-top: 10px;
- padding-left: 10px
- }
- .clsImg {
- border: solid 1px #ccc;
- padding: 3px;
- float: left
- }
- </style>
- <script type="text/javascript">
- $(function() {
- $("img").attr("src", function() {
- return "../Images/img0" + Math.floor(Math.random() * 2 + 1) + ".jpg"
- }); //设置src属性 通过随机生成数字。。。因此每次生成的Src都不同,从而实现页面上图片的随机切换。
- $("img").attr("title", "这是一幅风景画"); //设置title属性
- $("img").addClass("clsImg"); //增加样式
- })
- </script>
- </head>
- <body>
- <img alt="" src="../Images/img03.gif" style="float: left" />
- </body>
- </html>
评论暂时关闭