zoukankan      html  css  js  c++  java
  • new image的使用

    在看别人的程序,用到了new image()这种方法,然而怎么看也不是很明白;

    于是就上网搜,然而却没有一个人能够解开我心中的疑惑,因为没有一个人的程序是完整的,

    即使我知道怎么用,但是我看不到效果就是白搭......

    图像对象:

    建立图像对象:图像对象名称=new Image([宽度],[高度])

    图像对象的属性: border complete height hspace lowsrc name src vspace width

    图像对象的事件:onabort onerror onkeydown onkeypress onkeyup onload

    完整代码如下:

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        </head>
    <body>
        <div id="one"></div>
        <div></div>
        <script>
            var img=new Image();
        img.src="http://1.huanssky.applinzi.com/ablum/img/17.png";//如果不能用的话,改成自己的位置
        img.onload=function(){alert("img is loaded")};
        img.onerror=function(){alert("error!")};
        img.border="3px solid #ccc";
        function show(){alert("body is loaded");console.log(img.width+" "+img.height)};
        window.onload=show;
        document.getElementById("one").appendChild(img);
        </script>
    </body>
    
    </html>

    效果图如下:

    关于边框的那行代码有点问题,显示的是黑色。

    另外如果你想控制img的大小,可以这样创建var img=new Image(400,300);

    生成img之后,不要忘了将其添加到某个标签之下,开始我就是没有添加,代码可以运行,但是就是看不到图片。

    另外,从程序运行来看,先是图片加载成功,再是window.onload 事件触发。

  • 相关阅读:
    MFC下拉框使用方法
    C++学习之路,漫长而遥远
    [转载]浅析STL allocator
    [转载]《STL源码剖析》阅读笔记之 迭代器及traits编程技法
    回溯算法之n皇后问题
    算法练习之一
    Windows配置JDK and TOMCAT
    搭建openstack allinone版本
    在ms08_067_netapi中添加2003的Chinese版本
    msfvenom各平台木马生成
  • 原文地址:https://www.cnblogs.com/huansky/p/5570700.html
Copyright © 2011-2022 走看看