2016-09-14 145 views
2

我正在創建一個類似於StackOverflow的標記框。我發現這個問題已經在這裏提出(How to make a "tags box" using jQuery (with text input field + tags separated by comma)),但是我對Javascript有個疑問。創建標記框

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<div class='tag-container' id = "tag-container"> 
     <span class='dashfolio-tag'>Tag1</span> 
     <span class='dashfolio-tag'>Tag2</span> 
     <span class='dashfolio-tag'>Tag3</span> 
    </div>  


<input type="text" value="" placeholder="Add a tag" id = "add-tag-input" /> 

CSS:

.tag-container { 
max-width: 300px; /* helps wrap the tags in a specific width */ 

} 

.dashfolio-tag { 
    cursor:pointer; 
    background-color: blue; 
    padding: 5px 10px 5px 10px; 
    display: inline-block; 
    margin-top: 3px; /*incase tags go in next line, will space */ 
    color:#fff; 
    background:#789; 
    padding-right: 20px; /* adds space inside the tags for the 'x' button */ 
} 

.dashfolio-tag:hover{ 
    opacity:0.7; 
} 

.dashfolio-tag:after { 

position:absolute; 
content:"×"; 
padding:2px 2px; 
margin-left:2px; 
font-size:11px; 
} 

#add-tag-input { 
    background:#eee; 
    border:0; 
    margin:6px 6px 6px 0px ; /* t r b l */ 
    padding:5px; 
    width:auto; 
}   

JS:

$(document).ready(function(){ 
$(function(){ 

    $("#add-tag-input").on({ 
    focusout : function() { 
     var txt = this.value.replace(/[^a-z0-9\+\-\.\#]/ig,''); // allowed characters 
     if(txt) $("<span/>", {text:txt.toLowerCase(), insertBefore:this}); 


     this.value = ""; 
    }, 
    keyup : function(ev) { 
     // if: comma|enter (delimit more keyCodes with | pipe) 
     if(/(188|13)/.test(ev.which)) $(this).focusout(); 
    } 
    }); 
    $('.tag-container').on('click', 'span', function() { 
    if(confirm("Remove "+ $(this).text() +"?")) $(this).remove(); 
    }); 

}); 

}); 

我的問題很簡單,我怎麼添加新的輸入與dashfolio-tag類的跨度在#tag-container裏面?我試圖將insertBefore屬性添加到正確的節點,但無濟於事。在此先感謝你們!

+0

只是檢查,爲什麼不使用已經存在的庫?例如[Select2](https://select2.github.io/) –

回答

1

更改這行代碼,

if(txt) $("<span/>", {text:txt.toLowerCase(), insertBefore:this}); 

if(txt) { 
    $("<span/>", { 
     text:txt.toLowerCase(), 
     appendTo:"#tag-container", 
     class:"dashfolio-tag" 
    }); 
} 

觀看演示:https://jsfiddle.net/2gvdsvos/4/


要解決的利潤率在標籤之間,

更新HTML來,

<div> 
    <div class="tag-container" id="tag-container"> 
    <span class='dashfolio-tag'>tag1</span> 
    <span class='dashfolio-tag'>tag2</span> 
    <span class='dashfolio-tag'>tag3</span> 
    </div> 
</div> 
<div> 
    <input type="text" value="" placeholder="Add a tag" id="add-tag-input" /> 
</div> 

這添加到CSS,

.tag-container:after { 
    visibility: hidden; 
    display: block; 
    font-size: 0; 
    content: " "; 
    clear: both; 
    height: 0; 
} 

.dashfolio-tag { 
    ... 
    margin-right: 4px; 
    float: left; 
} 

希望這有助於! ;)

https://jsfiddle.net/2gvdsvos/5/

+0

太棒了!太感謝了!我注意到爲現有的標籤添加2個額外的標籤會弄亂邊緣,這在我的應用程序中也是如此。我檢查了HTML源代碼,它看起來都很正確。任何想法爲什麼發生這種情況? –

+1

這是因爲內聯元素之間具有默認的白色間距(這是內聯元素上缺省字體大小渲染的原因)。所以你必須添加沒有新行/間隔符的元素或使用不同的方法來解決它。例如浮動子元素 –