zoukankan      html  css  js  c++  java
  • 【JavaScript框架封装】实现一个类似于JQuery的内容框架的封装

    
    // 内容框架
    (function (xframe) {
        // 需要参与链式访问的(必须使用prototype的方式来给对象扩充方法)
        xframe.extend({
            /**
             * .html()用为读取和修改元素的HTML标签    对应js中的innerHTML
             * @return {html}
             */
            html: function () {
                var arg = arguments,
                    len = arg.length,
                    arr = Array.prototype.slice.call(this);
                if (this.length < 1) {
                    return this;
                }
    
                // 分为取值模式和设置模式
                if (len === 0) {
                    // 取值模式
                    return this[0].innerHTML;
                } else if (len === 1) {
                    // 设置模式
                    arr.each(function () {
                        this.innerHTML = arg[0];
                    });
                }
    
                return this;
    
            },
            /**
             * 用于获取文本信息
             * @return {*}
             */
            text: function () {
                var args = arguments,
                    len = args.length;
    
                if (this.length === 0) {
                    return this;
                }
    
                if (len === 0) {
                    // 取值模式
                    return this[0].innerText;
                } else if (len === 1) {
                    // 设置模式
                    this.each(function () {
                        this.innerText = args[0];
                    });
    
                }
                return this;
            },
            /**
             * 用于获取表单中的数值(input, form)
             * @return {*}
             */
            val: function () {
                // val();设置或者获取表单字段的值(前提是表单设置了value属性);
                var args = arguments,
                    len = args.length;
    
                if (this.length === 0) {
                    return this;
                }
    
                if (len === 0) {
                    return this[0].value;
                } else if (len === 1) {
                    this.each(function () {
                        this.value = args[0];
                    });
                }
    
                return this;
            }
        });
    
        // 不需要参与链式访问的
        xframe.extend(xframe, {});
    })(xframe);
  • 相关阅读:
    Google Go语言推出第一个正式版本:Go 1
    前端开发工作感悟:具体的量化指标
    AIR SDK 更新方法
    HTML5 MediaStream的运用
    理解css中的长度单位
    快速提高 Vi/Vim 使用效率的原则与途径
    Saving the Day with Scoped CSS
    事件的发生顺序HTML5移动开发
    BigPipe学习研究
    构建合规的Web应用程序
  • 原文地址:https://www.cnblogs.com/52tech/p/9343238.html
Copyright © 2011-2022 走看看