jQuery Show/Hide Div

前端 未结 3 801
星月不相逢
星月不相逢 2021-01-28 16:38

I\'m using this for a show/hide div expander, which is working fine, however, the HTML entities aren\'t being outputted.

$(document).ready(function() {
    $(\'.         


        
3条回答
  •  遥遥无期
    2021-01-28 16:54

    .text already does the conversion from "text" to "HTML" itself.

    From the documentation:

    We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), which replaces special characters with their HTML entity equivalents (such as < for <).

    So your text is being converted to &#9650; and thus being rendered, from your point-of-view, verbatim.

    You can encode the literal directly into the string (noting that the Javascript \u expects Hex, not Decimal):

    $(this).text("\u25B2 See Less");
    

    If you want to use the HTML entities:

    $(this).html("▲ See Less");
    

    See this jsfiddle.net snippet that shows:

    • Your approach
    • A valid approach with encoding the literal directly into the Javascript string
    • The approach with .html.

提交回复
热议问题