Matching jquery .text() to  

2019-03-17 23:46发布

问题:

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/

回答1:

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/



回答2:

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.