jQuery Show/Hide Div

前端 未结 3 798
星月不相逢
星月不相逢 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 &amp;#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("&#9650; 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.
    0 讨论(0)
  • 2021-01-28 16:54

    .text() doesn't process any html entity. You must use .html() in this case.

    0 讨论(0)
  • 2021-01-28 17:13

    Have you tried using .html rather then .text

    $(document).ready(function() {
        $('.slickbox').hide();
        $("#slick-toggle").toggle(function() {
            $(this).html("&#9650; See Less");
            $('.slickbox').slideToggle(500);
        }, function() {
            $(this).html("See More &#9660;");
            $('.slickbox').slideToggle(500);
        });
    });
    
    0 讨论(0)
提交回复
热议问题