2016-09-21 39 views
0

如何檢查/刪除元素?

$(function(){ 
 

 
    $("#tags 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|32)/.test(ev.which)){ 
 
     $(this).focusout(); 
 
     } else if (/(8)/.test(ev.which) && this.value == '' && $(this).prev("span").css('background-color')=="rgb(225, 0, 0)") { 
 
     $(this).prev("span").remove(); 
 
     } else if (/(8)/.test(ev.which) && this.value == '') { 
 
     $(this).prev("span").css('background-color', 'Red'); 
 
     } 
 
    } 
 
    }); 
 
    $('#tags').on('click', 'span', function() { 
 
    $(this).remove(); 
 
    }); 
 

 
});
#tags{ 
 
    float:right; 
 
    border:1px solid #ccc; 
 
    padding:5px; 
 
    font-family:Arial; 
 
} 
 
#tags > span{ 
 
    cursor:pointer; 
 
    display:block; 
 
    float:right; 
 
    color:#3e6d8e; 
 
    background:#E1ECF4; 
 
    padding:5px; 
 
    padding-right:25px; 
 
    margin:4px; 
 
} 
 
#tags > span:hover{ 
 
    opacity:0.7; 
 
} 
 
#tags > span:after{ 
 
position:absolute; 
 
content:"×"; 
 
border:1px solid; 
 
padding:2px 5px; 
 
margin-left:3px; 
 
font-size:11px; 
 
} 
 
#tags > input{ 
 
    direction: rtl; 
 
    background:#eee; 
 
    border:0; 
 
    margin:4px; 
 
    padding:7px; 
 
    width:auto; 
 
} 
 
.autocomplete{ 
 
    display:none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id="tags"> 
 
    <span>php</span> 
 
    <span>html</span> 
 
    <input type="text" value="" placeholder="Add a tag" /> 
 
    <div class="autocomplete"></div> 
 
</div>

欲通過按下退格鍵(當輸入爲聚焦)或通過點擊該標籤刪除的標籤。現在我正在談論第一個選項。此外,我需要先將其設置爲紅色背景色(如「你確定嗎?」概念)。好吧,就像你看到的那樣,我的小提琴把它設置成了一個紅色的背景色,但它並沒有將它移除。怎麼了?

+1

添加一個類來代替。 jQuery的css()方法返回依賴於瀏覽器的計算風格,你不應該依賴它。無論如何,你的代碼似乎很明顯調試 –

+1

作爲一個方面說明,225!= 255 ... –

+0

@ A.Wolff那麼你是什麼意思,我不能依靠它呢?我如何在所有瀏覽器中獲取元素的顏色?像'if($(this).css(「background-color」)==「Red」){}'是錯誤的嗎? –

回答

1

你必須更好地切換類和檢查它,e.g:

$(function() { 
 

 
    $("#tags 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|32)/.test(ev.which)) { 
 
     $(this).focusout(); 
 
     } else if (/(8)/.test(ev.which) && this.value == '' && $(this).prev("span").hasClass('toRemove')) { //<< check for class 
 
     $(this).prev("span").remove(); 
 
     } else if (/(8)/.test(ev.which) && this.value == '') { 
 
     $(this).prev("span").addClass('toRemove'); //<< add class 
 
     } else { 
 
     $(this).prevAll('.toRemove').removeClass('toRemove'); //<< remove class on keyup 
 
     } 
 
    } 
 
    }); 
 
    $('#tags').on('click', 'span', function() { 
 
    $(this).remove(); 
 
    }); 
 

 
});
#tags { 
 
    float: right; 
 
    border: 1px solid #ccc; 
 
    padding: 5px; 
 
    font-family: Arial; 
 
} 
 
#tags > span { 
 
    cursor: pointer; 
 
    display: block; 
 
    float: right; 
 
    color: #3e6d8e; 
 
    background: #E1ECF4; 
 
    padding: 5px; 
 
    padding-right: 25px; 
 
    margin: 4px; 
 
} 
 
#tags > span:hover { 
 
    opacity: 0.7; 
 
} 
 
#tags > span:after { 
 
    position: absolute; 
 
    content: "×"; 
 
    border: 1px solid; 
 
    padding: 2px 5px; 
 
    margin-left: 3px; 
 
    font-size: 11px; 
 
} 
 
#tags > input { 
 
    direction: rtl; 
 
    background: #eee; 
 
    border: 0; 
 
    margin: 4px; 
 
    padding: 7px; 
 
    width: auto; 
 
} 
 
.autocomplete { 
 
    display: none; 
 
} 
 
#tags > span.toRemove { 
 
    background-color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="tags"> 
 
    <span>php</span> 
 
    <span>html</span> 
 
    <input type="text" value="" placeholder="Add a tag" /> 
 
    <div class="autocomplete"></div> 
 
</div>

+0

謝謝.. upvote。 。但是有一個小問題..我認爲你的代碼會更加用戶友好,如果在輸入內容之後紅色標籤是正常的顏色標籤。*(不等到一個新的標籤添加)* –

+0

@MartinAJ Ya,剛剛更新了它 –

+0

現在,在該輸入中寫入一個字符,然後按* backspace *一次,然後這兩個字符將被刪除,最後一個標記將是紅色。當inp時,最後一個標籤需要多一個退格ut是空的... –

1

我建立在你的計數器中,檢查你的按鍵狀態。如果您按一次計數器爲1,並且如果第二次按下,它將刪除該元素,並再次爲0。我希望那就是你想要的。檢查:

$(function() { 

    counter = 0; 

    $("#tags 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 = ""; 
     $("#tags span").css('background-color', '#E1ECF4'); 
     counter = 0; 
    }, 
    keyup: function(ev) { 
     // if: comma|enter (delimit more keyCodes with | pipe) 
     if (/(188|13|32)/.test(ev.which)) { 
     $(this).focusout(); 
     } else if (/(8)/.test(ev.which) && this.value == '') { 
     counter = counter + 1; 
     if (counter == 1) { 
      $(this).prev("span").css('background-color', 'Red'); 
     } else { 
      $(this).prev("span").remove(); 
      counter = 0; 
     } 
     } 
    } 
    }); 
    $('#tags').on('click', 'span', function() { 
    $(this).remove(); 
     $("#tags span").css('background-color', '#E1ECF4'); 
     counter = 0; 
    }); 

}); 

https://jsfiddle.net/4swtduae/7/

+0

是的,現在這是很多更好..但它會是完美的,如果紅色消失,當你在輸入*除了退格鍵之外任何東西*(我的意思是它不應該等到一個新的標籤添加刪除紅色)* –