2017-08-06 74 views
1

我想要使用代碼&#43切換+-符號;和-HTML符號代碼不能在toggle html中使用jQuery函數

我嘗試了以下解決方案來切換HTML,但它似乎不適用於這些代碼。它切換一次,但不會切換回來。

JSFiddle

HTML

<p>&#43;</p> 

jQuery的

jQuery('p').on('click', function(){ 
    jQuery('p').html(jQuery('p').html() === '&#45;' ? '&#43;' : '&#45;'); 
}); 

我怎樣才能解決這個問題,從而撥動作品?

回答

0

我不知道你爲什麼要在這種情況下使用ASCII碼,但你可以實現你的條件,如:

jQuery('p').on('click', function() { 
 
    jQuery('p').html(jQuery('p').html() === '-' ? '&#43;' : '&#45;'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<p>&#43;</p>

或者,如果你想使用只是ASCII碼,你可以先轉換值,則比較喜歡:

jQuery('p').on('click', function(){ 
 
    var ascii = '&#'+jQuery('p').html().charCodeAt(0)+';'; 
 
    
 
    jQuery('p').html(ascii === '&#45;' ? '&#43;' : '&#45;'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<p>&#43;</p>

希望這會有所幫助。