2017-08-06 58 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>

希望这会有所帮助。