李承鹏博客文章|仿新浪博客文章发布时为文章添加标签实现方法

更新时间:2018-08-31    来源:文本特效    手机版     字体:

【www.bbyears.com--文本特效】

仿新浪qing文章发布时为文章添加标签

可以通过点击下面的 常用标签 添加 也可以通过文本框输入添加,回车后标签就会被贴上,添加时会自动判断当前标签是否已经添加,如果添加过刚不重复添加..没有则添加,限定最多添加5个标签。
HTML:

 代码如下

 
            文章标签:
           
               
               
               
               
 
               
                    常用标签:
                   
                    标签风景音乐
               
           
 
       

CSS:

 代码如下

 *{
                margin: 0;
                padding: 0;
            }
            html,body{
                background:#fff;
            }
            #page{
                padding: 20px;
            }
            .clearfix{
                clear:both;
            }
            .tagbox{
                border: 1px solid #ccc;
                width: 200px;
 
                height: 200px;
                padding: 10px;
                position: relative;
            }
            .tag{
                background:lightblue;
                color: #fff;
                padding: 2px 5px;
                display: block;
                float: left;
                margin:5px 5px 5px 0;
            }
            #tags{
                margin-bottom: 10px;
                overflow-y: auto;
            }
            .old{
                position: absolute;
                bottom: 5px;
                left: 5px;
            }
            .tagbox span{
                cursor: pointer;
            }

JS:

 代码如下

 $(function(){
                $(".tagbox").click(function(){
                    $("input[name="tag"]").focus();
                })
                $(document).on("click", "#tags span", function(){
                    $(this).remove();
                });
                $(".old span").click(function(){
                    ids=new Array();
                    var txt=$(this).attr("name");
                    var id=$(this).attr("id")
                    $("#tags .tag").each(function(){
                        ids+=$(this).attr("id")+","
                    });
                    if(ids==""){
                        ids=new Array();
                    }else{
                        ids = ids.split(",");
                    }
                    if(ids.length>5){
                        alert("标签最多添加5个哦!");
                        return false;
                    };
                    var exist=$.inArray(id,ids);
                    if(exist<0){
                        $("#tags").append(""+txt+"")
                    }
                })
                $("input[name="tag"]").bind("keyup",function(event){
                    if(event.keyCode==13){ 
                        var txt=$(this).val();
                        if(txt!=""){
                            txts=new Array();
                            $("#tags .tag").each(function(){
                                txts+=$(this).attr("name")+","
                            });
                            if(txts==""){
                                txts=new Array();
                            }else{
                                txts = txts.split(",");
                            }
                            if(txts.length>5){
                                alert("标签最多添加5个哦!");
                                return false;
                            };
                            var exist=$.inArray(txt,txts);
                            if(exist<0){
                                $("#tags").append(""+txt+"")
                                $(this).val("");
                            }else{
                                $(this).val("");
                            }
                        }
                    }    
                });  
            })

本文来源:http://www.bbyears.com/wangyetexiao/43991.html

猜你感兴趣

热门标签

更多>>

本类排行