web前端之jQuery脚本的书写

86 篇文章 0 订阅
14 篇文章 0 订阅

web前端之jQuery脚本的书写

今天要尝试编写一个jquery脚本,其主要用于为

如果a标签title里面没有值,则添加对应的title,当有title时则不添加

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript" src="./jquery-3.2.1.js"></script>
<script type="text/javascript" src="./index.js"></script>
<script type="text/javascript">
$(function (){
    $('a').title();
})
</script>
</head>
<body>
    <a href="#" title="1">有标签</a><br>
    <a href="#" >没有标签</a><br>
    <a href="#" title="2">有标签</a><br>
    <a href="#" >没有标签</a><br>
    <a href="#" >没有标签</a><br>
    <a href="#" title="3">有标签</a><br>
    <a href="#" >没有标签</a>
</body>
</html>
;(function($){
    $.fn.title=function(){
        var that=this;
        $.each(that,function(index,value){
            if(that[index].title===''){
                that[index].title="123";
            }
        }); 
    };
})(jQuery);

这样就能简单地实现一个基础的jquery脚本。现在我们需要进阶一下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript" src="./jquery-3.2.1.js"></script>
<script type="text/javascript" src="./index.js"></script>
<script type="text/javascript">
$(function (){
    // $('a').title();
    $('a').title("asd");
    // $('a').title('set','德玛西亚');
    // $('a').title('delete');
    // $('a').title('get');
    // $('a').title('setAll','德玛西亚');
})
</script>
</head>
<body>
    <a href="#" title="1">有标签</a><br>
    <a href="#" >没有标签</a><br>
    <a href="#" title="2">有标签</a><br>
    <a href="#" >没有标签</a><br>
    <a href="#" >没有标签</a><br>
    <a href="#" title="3">有标签</a><br>
    <a href="#" >没有标签</a>
</body>
</html>
;(function($){
    $.fn.title=function(){
        var result = null;
        var atitle = arguments[0];
        if(arguments.length > 0){
            result = this.each(function (index,item){
                var title = $(item).attr('title');
                title = title || atitle;
                $(item).attr('title',title);
            })
        }else{
            result = this.each(function (index,item){
                var title = $(item).attr('title');
                title = title;
                $(item).attr('title',title);
            })
        }
        return result;
    };
})(jQuery);

虽然达到了我们需要的设置的数据,但是还能更深入哦:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript" src="./jquery-3.2.1.js"></script>
<script type="text/javascript" src="./index.js"></script>
<script type="text/javascript">
$(function (){
    // $('a').title();
    // $('a').title("asd");
    $('a').title('set','德玛西亚');
    $('a').title('delete');
    $('a').title('get');
    $('a').title('setAll','德玛西亚');
})
</script>
</head>
<body>
    <a href="#" title="1">有标签</a><br>
    <a href="#" >没有标签</a><br>
    <a href="#" title="2">有标签</a><br>
    <a href="#" >没有标签</a><br>
    <a href="#" >没有标签</a><br>
    <a href="#" title="3">有标签</a><br>
    <a href="#" >没有标签</a>
</body>
</html>
;(function($){
    $.fn.title=function(){
        var result=null;
        var atitle=arguments[0];
        var avalue=arguments[1];
        if(arguments.length > 0){
            if(atitle==='set'){
                result = this.each(function (index,item){
                    var title =$(item).attr('title');
                    title = title || avalue;
                    $(item).attr('title',title);
                })
            }else if(atitle==='get'){
                result = this.each(function (index,item){
                    var title =$(item).attr('title');
                    console.log(title);
                })
            }else if(atitle==='delete'){
                result = this.each(function (index,item){
                    $(item).removeAttr('title');
                })
            }else if(atitle==='setAll'){
                result = this.each(function (index,item){
                    $(item).attr('title',avalue);
                })
            }
        }else{
            result = this.each(function (index,item){
                var title =$(item).attr('title');
                title = title;
                $(item).attr('title',title);
            })
        }
        return result;
    };
})(jQuery);

学的不够深入,jquery脚本书写到此就结束了

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值