Matching jquery .text() to  

2019-03-17 23:45发布

Due to circumstances out of my control, SharePoint, I have the following piece of code.

var item = $('<span><font size=1>&nbsp;</font></span>').text()

I am trying to compare the .text() value to &nbsp; and don't know what to do. Stepping through the code item seems to equal " " which makes sense. But doing item == " " returns false. How should this comparison be done?

EDIT: Example fiddle

http://jsfiddle.net/hUBeP/2/

2条回答
做自己的国王
2楼-- · 2019-03-18 00:19

Compare it to &nbsp;, as that is the content you want to match. It appears not to be that because the browser renders is as space, rather than characters. Comparing it to &nbsp; should result in either two sets of &nbsp;s being compared, or two identical spaces being compared.

查看更多
Deceive 欺骗
3楼-- · 2019-03-18 00:20

Try checking for '\xa0' (which is the character created by &nbsp;):

var item = $("<span><font size=1>&nbsp;</font></span>").text();
alert("'" + item + "' " + (item == '\xa0'));

http://jsfiddle.net/hUBeP/3/

查看更多
登录 后发表回答