What is the difference between jQuery: text() and html() ?

后端 未结 16 1470
傲寒
傲寒 2020-11-22 03:45

What the difference between text() and html() functions in jQuery ?

$(\"#div\").html(\'Linkhello\         


        
相关标签:
16条回答
  • 2020-11-22 04:31

    Use .text(…) when you intend to display the value as a simple text.

    Use .html(…) when you intend to display the value as a html formatted text (or HTML content).

    You should definitely use .text(...) when you’re not sure that your text (e.g. coming from an input control) do not contain characters/tags that has special meaning in HTML. This is really important because this could result in the text will not display properly but it could also cause that an unwanted JS script snippet (e.g. coming from another user input) to be activated.

    0 讨论(0)
  • 2020-11-22 04:32

    The text() method entity-escapes any HTML that is passed into it. Use text() when you want to insert HTML code that will be visible to people who view the page.

    Technically, your second example produces:

    <a href="example.html">Link</a><b>hello</b>
    

    which would be rendered in the browser as:

    <a href="example.html">Link</a><b>hello</b>
    

    Your first example will be rendered as an actual link and some bold text.

    0 讨论(0)
  • 2020-11-22 04:32

    Strange that no-one mentioned the Cross Site scripting prevention benefit of .text() over .html() (Although others have just mentioned that .text() escapes the data).

    It's always recommended to use .text() when you want to update data in DOM which is just data / text for the user to see.

    .html() should be mostly used to get the HTML content within a div.

    0 讨论(0)
  • 2020-11-22 04:33

    ((please update if necessary, this answer is a Wiki))

    Sub-question: when only text, what is faster, .text() or .html()?

    Answer: .html() is faster! See here a "behaviour test-kit" for all the question.

    So, in conclusion, if you have "only a text", use html() method.

    Note: Doesn't make sense? Remember that the .html() function is only a wrapper to .innerHTML, but in the .text() function jQuery adds an "entity filter", and this filter naturally consumes time.


    Ok, if you really want performance... Use pure Javascript to access direct text-replace by the nodeValue property. Benchmark conclusions:

    • jQuery's .html() is ~2x faster than .text().
    • pure JS' .innerHTML is ~3x faster than .html().
    • pure JS' .nodeValue is ~50x faster than .html(), ~100x than .text(), and ~20x than .innerHTML.

    PS: .textContent property was introduced with DOM-Level-3, .nodeValue is DOM-Level-2 and is faster (!).

    See this complete benchmark:

    // Using jQuery:
    simplecron.restart(); for (var i=1; i<3000; i++) 
        $("#work").html('BENCHMARK WORK');
    var ht = simplecron.duration();
    simplecron.restart(); for (var i=1; i<3000; i++) 
        $("#work").text('BENCHMARK WORK');
    alert("JQuery (3000x): \nhtml="+ht+"\ntext="+simplecron.duration());
    
    // Using pure JavaScript only:
    simplecron.restart(); for (var i=1; i<3000; i++)
        document.getElementById('work').innerHTML = 'BENCHMARK WORK';
    ht = simplecron.duration();
    simplecron.restart(); for (var i=1; i<3000; i++) 
        document.getElementById('work').nodeValue = 'BENCHMARK WORK';
    alert("Pure JS (3000x):\ninnerHTML="+ht+"\nnodeValue="+simplecron.duration());
    
    0 讨论(0)
  • 2020-11-22 04:36
    var v = "&#x5168;&#x540D;";
    $("#div").html(v); //display as "全名"
    $("#div").text(v); //display as "&#x5168;&#x540D;"
    

    text() – This method sets or returns the text content of elements selected. html() – This method sets or returns the content of elements selected. Refer example here : https://www.tutorialspoint.com/online_jquery_editor.php

    0 讨论(0)
  • 2020-11-22 04:38

    The different is .html() evaluate as a html, .text() avaluate as a text.
    Consider a block of html
    HTML

    <div id="mydiv">
    <div class="mydiv">
        This is a div container
        <ul>
          <li><a href="#">Link 1</a></li>
          <li><a href="#">Link 2</a></li>
        </ul>
        a text after ul
    </div>
    </div>
    

    JS

    var out1 = $('#mydiv').html();
    var out2 = $('#mydiv').text();
    console.log(out1) // This output all the html tag
    console.log(out2) // This is output just the text 'This is a div container Link 1 Link 2 a text after ul'
    

    The illustration is from this link http://api.jquery.com/text/

    0 讨论(0)
提交回复
热议问题