我在内容可编辑段落
我有一个带有contenteditable="true"
的段落<p>
,它适用于我创建的每个选项,例如粗体,斜体,BGCOLOR,...
我在内容可编辑段落
中键入空格,当我通过以下代码将该内容可编辑段落的内容与字符串进行比较时,它会返回我 false!
console.log($('p[contenteditable="true"]').text()==" ");
// return false
console.log($('p[contenteditable="true"]').text());
// return " " char Code of this space is 160
这太奇怪了!为什么会这样?我已经尝试了上面的代码.textContent
并且发生了相同的结果
我已经测试了您发布的内容,它正在工作。见 jsfiddle
.HTML:
<p contenteditable="true"> </p>
Javascript:
console.log(jQuery('p[contenteditable="true"]').text());
console.log(jQuery('p[contenteditable="true"]').text() == ' ');
console.log(jQuery('p[contenteditable="true"]').text().charCodeAt(0));
返回:
" "
true
32
请检查您实际上输入的是一个空格(字符代码 32)而不是不同的字符。
溶液:
if(jQuery('p[contenteditable="true"]').text().charCodeAt(0) == 160){
console.log('Space!');
}
请参阅: HTML 字符代码