我的 VF 页面中存在 javascript 问题,因此我在此发布此问题。
这是我的代码:
<html>
<input id="xyz" value=" "/>
<script>
alert( document.getElementById('xyz').value);
var x = document.getElementById('xyz').value;
if( x == ' ' )
alert(1)
else
alert(2);
</script>
</html>
输入字段 xyz 的值为
我正在检查 javascript 中的值,但 if 条件永远不会计算为 true。可能是什么问题呢? jsfiddle
最佳答案
静态 String.fromCharCode()
方法返回使用指定的 Unicode 值序列创建的字符串。
对于不间断空格,您可以使用String.fromCharCode(160)
if( x ==String.fromCharCode(160))
alert(1)
else
alert(2);
演示: http://jsfiddle.net/36eyp/
引用号:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
HTML:
<span id="xyz" > </span>
Javascript:
alert( document.getElementById('xyz').innerHTML);
var x = document.getElementById('xyz').innerHTML;
if( x == ' ' )
alert(1)
else
alert(2);
http://www.adamkoch.com/2009/07/25/white-space-and-character-160/
关于javascript - 无法使用 javascript if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21133593/