I am trying to convert an unordered list into a <select> box for people with small resolutions, indenting the text depending on the level in the list. To do this, I prepend the string with &nbsp;. However it looks like jQuery, for some reason, is double HTML-encoding this into &amp;nbsp;. How do I prevent this behavior and use a literal &nbsp; (i.e. so that a non-breaking space character is shown to the browser):

var text = '';
var i;

for (i = 0; i < level; i++) {
    text += '&nbsp;';
}
text += el.text();

if (el.hasClass('noclick')) {
    $('<optgroup />', {
        'label' : text
    }).appendTo('#menu select');
}
else {
    $('<option />', {
        'value' : el.attr('href'),
        'text' : text
    }).appendTo('#menu select');
}

Here's my jsFiddle.

有帮助吗?

解决方案

Try using \u00A0 (the non-breaking space character) instead of &nbsp;.

其他提示

You can insert the literal non-break space character in your JavaScript scripts with \xA0:

text += '\xA0';
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top