Jquery简化了传统JS实现功能的代码量,例如:

jquery的$(function(){});相当于传统JS的window.onload=function(){};

还是看一下我写的代码吧:

<script type="text/javascript">
        window.onload = function() {
            //定义一个类对象
            var Zhang = {
                Title: "",
                Width: "100",
                Height: "",
                Zhang: function(title, width, height) {
                    this.Title = title;
                    this.Width = width;
                    this.Height = height;
                },
                Zhang: function(title, height) {//方法的重载
                    this.Title = title;
                    this.Height = height;
                },
                init: { target: "_blank", src: "" },
                generate: function() {
                    var insertedNode = document.createElement('img'); //添加HTML標記
                    insertedNode.setAttribute('src', Zhang.init.src);
                    insertedNode.setAttribute("width", Zhang.Width);
                    insertedNode.setAttribute("height", Zhang.Height);

// var newTextNode = document.createTextNode("<img src='" + Zhang.init.src

// + "' width=" + Zhang.Width + " height=" + Zhang.Height + ">"); //添加文本

                    document.getElementsByTagName("body")[0].appendChild(insertedNode);
                }
            }
 
            var obj = document.getElementById("add");
            Zhang.Zhang("图像测试", "300", "600"); //覆盖原来的值100
            Zhang.init.src = "http://d4.sina.com.cn/201106/03/312669_78090-biaoxian.JPG";
            if (typeof (obj.addEventListener) != "undefined") { //判断浏览器
                obj.addEventListener("click", Zhang.generate, true); //触发单击事件
            } else {
                obj.attachEvent("onclick", Zhang.generate); //绑定方法
            }
 
        };
    </script>

相关文章:

  • 2022-01-12
  • 2021-09-12
  • 2021-12-23
  • 2022-12-23
  • 2021-09-14
  • 2021-05-28
猜你喜欢
  • 2021-09-23
  • 2021-06-30
  • 2022-01-23
  • 2021-08-04
  • 2021-12-03
  • 2022-12-23
  • 2021-11-18
相关资源
相似解决方案