美文网首页跨平台
jQuery篇之常用函数

jQuery篇之常用函数

作者: 平安喜乐698 | 来源:发表于2019-10-23 11:11 被阅读0次
目录
        1. 获取 浏览器的名称、版本 , 是否属于W3C盒子模型
        2. 检测对象是否为空 , 是否为原始对象
        3. 检测两个节点的包含关系
        4. 删除字符串中左右两边的空格符
        5. 使对象或数组按照key/value格式进行序列化编码
  1. 获取 浏览器的名称、版本 , 是否属于W3C盒子模型

$.browser (获取浏览器的名称、版本等)

浏览器版本号
    $.browser.version 

浏览器的名称
    $.browser.chrome 为true,则为谷歌
    $.browser.mozilla  为true,则为火狐
例

<!DOCTYPE html>
<html>
    <head>
        <title>获取浏览器名称和版本号</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script src="https://www.imooc.com/data/jquery-1.8.2.min.js" type="text/javascript"></script>
    </head>
    
    <body>
        <div id="divtest">
            <div class="title">
                <span class="fl">获取浏览器名称和版本号</span> 
            </div>
            <div class="content"></div>
        </div>
        
        <script type="text/javascript">
            $(function () {
                var strTmp = "您的浏览器名称是:";
                if ($.browser.chrome) { //谷歌浏览器
                    strTmp += "Chrome";
                }
                if ($.browser.mozilla) { //火狐相关浏览器
                    strTmp += "Mozilla FireFox";
                }
                strTmp += "<br /><br /> 版本号是:" //获取版本号
                       +$.browser.version;
                $(".content").html(strTmp);
            });
        </script>
    </body>
</html>

$.support.boxModel (检测浏览器是否属于W3C盒子模型)

    浏览器的盒子模型分为两类,一类为标准的w3c盒子模型,另一类为IE盒子模型,两者区别为在Width和Height这两个属性值中是否包含padding和border的值。
    w3c盒子模型不包含,IE盒子模型则包含。

$.support.boxModel  为true,则为w3c盒子模型
例

<html>
    <head>
        <title>检测是否是盒子模型</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script src="https://www.imooc.com/data/jquery-1.8.2.min.js" type="text/javascript"></script>
    </head>
    
    <body>
        <div id="divtest">
            <div class="title">
                <span class="fl">检测是否是盒子模型</span> 
            </div>
            <div class="content"></div>
        </div>
        
        <script type="text/javascript">
            $(function () {
                var strTmp = "您打开的页面是:";
                if ($.support.boxModel) { //是W3C盒子模型
                    strTmp += "W3C盒子模型";
                }
                else { //是IE盒子模型
                    strTmp += "IE盒子模型";
                }
                $(".content").html(strTmp);
            });
        </script>
    </body>
</html>
  1. 检测对象是否为空 , 是否为原始对象

$.isEmptyObject(obj) (检测对象是否为空)

$.isEmptyObject(obj)    对象为空则为true
例

<html>
    <head>
        <title>检测对象是否为空</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script src="https://www.imooc.com/data/jquery-1.8.2.min.js" type="text/javascript"></script>
    </head>
    
    <body>
        <div id="divtest">
            <div class="title">
                <span class="fl">检测对象是否为空</span> 
            </div>
            <div class="content"></div>
        </div>
        
        <script type="text/javascript">
            $(function () {
                var obj = { "姓名": "土豪一族" };
                var strTmp = "您定义了一个:";
                if ($.isEmptyObject(obj)) { //检测是否为空
                    strTmp += "空对象";
                }
                else {
                    strTmp += "非空对象";
                }
                $(".content").html(strTmp);
            });
        </script>
    </body>
</html>

$.isPlainObject (obj) (检测对象是否为原始对象)

$.isPlainObject (obj)   为true,则是通过{}或new Object()关键字创建的原始对象
例

<html>
    <head>
        <title>检测对象是否为原始对象</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script src="https://www.imooc.com/data/jquery-1.8.2.min.js" type="text/javascript"></script>
    </head>
    
    <body>
        <div id="divtest">
            <div class="title">
                <span class="fl">检测对象是否为原始对象</span> 
            </div>
            <div class="content"></div>
        </div>
        
        <script type="text/javascript">
            $(function () {
                var obj = "null";
                var strTmp = "您定义了一个:";
                if ($.isPlainObject (obj)) { //检测是否为原始对象
                    strTmp += "原始对象";
                }
                else {
                    strTmp += "非原始对象";
                }
                $(".content").html(strTmp);
            });
        </script>
    </body>
</html>
  1. $.contains (container, contained) (检测两个节点的包含关系)
$.contains (container, contained)    为true则包含
例

<!DOCTYPE html>
<html>
    <head>
        <title>检测两个节点的包含关系</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script src="https://www.imooc.com/data/jquery-1.8.2.min.js" type="text/javascript"></script>
    </head>
    
    <body>
        <div id="divtest">
            <div class="title">
                <span class="fl">检测两个节点的包含关系</span> 
            </div>
            <div class="content"></div>
        </div>
        
        <script type="text/javascript">
            $(function () {
                var node_a = document.body.firstChild;
                var node_b = document.body;
                var strTmp = "对象node_a";
                if ($.contains (node_b, node_a)) { //检测是否包含节点
                    strTmp += " 包含 ";
                }
                else {
                    strTmp += " 不包含 ";
                }
                strTmp += "对象node_b";
                $(".content").html(strTmp);
            });
        </script>
    </body>
</html>
  1. $.trim (str) (删除字符串中左右两边的空格符)
$.trim (str) 
    能删除字符串中左右两边的空格符,但该函数不能删除字符串中间的空格
<!DOCTYPE html>
<html>
    <head>
        <title>字符串操作函数</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script src="https://www.imooc.com/data/jquery-1.8.2.min.js" type="text/javascript"></script>
    </head>
    
    <body>
        <div id="divtest">
            <div class="title">
                <span class="fl">字符串操作函数</span> 
                <span class="fr">
                    <input id="btnShow" name="btnShow" type="button" value="计算" />
                </span>
            </div>
            <div class="content">
                <input id="txtName" name="txtName" type="text" />
                <div class="tip"></div>
            </div>
        </div>
        
        <script type="text/javascript">
            $(function () {
                $("#btnShow").bind("click", function () {
                    $(".tip").html("");
                    var strTmp = "内容:";
                    var strOld = $("#txtName").val();
                    var strNew =$.trim(strOld);
                    strTmp += strOld;
                    strTmp += "<br/><br>除掉空格符前的长度:"
                    strTmp += strOld.length;
                    strTmp += "<br/><br>除掉空格符后的长度:"
                    strTmp += strNew.length;
                    $(".tip").show().append(strTmp);
                });
            });
        </script>
    </body>
</html>
  1. $.param (obj) (使对象或数组按照key/value格式进行序列化编码)
$.param (obj)  
    能使对象或数组按照key/value格式进行序列化编码,用于向服务端发送URL请求
    .serialize仅能用于form提交的数据转换
例

<!DOCTYPE html>
<html>
    <head>
        <title>URL操作函数</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script src="https://www.imooc.com/data/jquery-1.8.2.min.js" type="text/javascript"></script>
    </head>
    
    <body>
        <div id="divtest">
            <div class="title">
                <span class="fl">URL操作函数</span> 
            </div>
            <div class="content">
                <div class="tip"></div>
            </div>
        </div>
        
        <script type="text/javascript">
            $(function () {
                //基本信息对象
                var objInfo = new Object();
                objInfo.name = "白富美";
                objInfo.sex = 1;
                //序列化对象
                var objNewInfo =$.param (objInfo);
                //显示序列化后的对象
                var strTmp = "<b>对象 白富美 序列化后</b>:<br/><br/>";
                strTmp += objNewInfo;
                //显示在页面中
                $(".tip").show().append(strTmp);
            });
        </script>
    </body>
</html>

相关文章

  • jQuery API学习之CSS操作函数与动画效果篇

    jQuery CSS操作函数 常用jQuery CSS操作函数介绍: jQuery 常用特效API: jQuery...

  • jQuery篇之常用函数

    1. 常用函数 浏览器的名称、版本 , 是否属于W3C盒子模型 $.browser (获取浏览器的名称、版本等) ...

  • jQuery常用方法以及ajax

    jQuery常用函数 .each(function(index,Element)) 遍历一个jQuery对象,为每...

  • jQuery常用函数

    1、获取浏览器的名称与版本信息 在jQuery中,通过$.browser对象可以获取浏览器的名称和版本信息 $.b...

  • jQuery常用函数

    常用方法 .each(function(index.Element)) 遍历一个jQuery对象,为每个匹配元素执...

  • Jquery 常用函数

    一 操作类的方法1.addClass(className)2.removeClass(className)3.ha...

  • JQuery常用函数

    最近使用得比较多的jq函数 记一下 .addClass 函数用为当前对象所匹配的每一个元素添加指定的css 样式 ...

  • jQuery DOM教辅

    jQuery 常用的函数 一、jQuery DOM 元素方法 .get() 获得由选择器指定的 DOM 元素。 ....

  • jQuery核心函数“$();”的使用

    jQuery中最常用的函数就是$()函数了,至于为什么是使用这个$符号,之前老师开了个玩笑说:jQuery的开发者...

  • 深入理解立即执行函数 2018-04

    看过jQuery源码的人应该知道,jQuery开篇用的就是立即执行函数。立即执行函数常用于第三方库,好处在于隔离作...

网友评论

    本文标题:jQuery篇之常用函数

    本文链接:https://www.haomeiwen.com/subject/wmmhvctx.html