Is there a php echo/print equivalent in javascript

前端 未结 10 1171
暖寄归人
暖寄归人 2020-12-24 00:33

Say I want to print html from inside a script tag.

A source like this

foo
相关标签:
10条回答
  • 2020-12-24 01:03
    $('element').html('<h1>TEXT TO INSERT</h1>');
    

    or

    $('element').text('TEXT TO INSERT');
    
    0 讨论(0)
  • 2020-12-24 01:07

    From w3school's page on JavaScript output,

    JavaScript can "display" data in different ways:

    Writing into an alert box, using window.alert().

    Writing into the HTML output using document.write().

    Writing into an HTML element, using innerHTML.

    Writing into the browser console, using console.log().

    0 讨论(0)
  • 2020-12-24 01:10

    You can use document.write, however it's not a good practice, it may clear the entire page depends on when it's being executed.

    You should use Element.innerHtml like this:

    <div>foo</div>
    <span id="insertHere"></span>
    <div>bar</div>
    
    <script>
    document.getElementById('insertHere').innerHTML = '<div>Print this after the script tag</div>';
    </script>
    
    0 讨论(0)
  • 2020-12-24 01:10

    l('output text example')

    after once defining

    l=console.log
    

    seems fine to me for a quick hacksy JS debugging in the browser console.

    Based on @Lil' Bits’s answer – thanks!

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