简体   繁体   中英

Update an HTML tag innerHTML without line break

I want to add a variable from javascript to HTML page. After the script is executed, it adds a new line (before the second sentence). What I want to do is to update the HTML inside the <div> tag only and continue the line without any break.

    <html> 
    <body> 
    <p> Some text here <div id="my-id">my text</div>. Other text here.</p>
    <script>
    document.getElementById("my-id").innerHTML = "Add my text here";
    </script>
    </body>
    </html>

A <div> is a block-level element by default.

You can override this with CSS, but you're better off using a <span> instead, which is not a block-level element by default.

<span id='my-id'></span>

If you absolutely must use a div, you can use inline style:

<div style='display:inline' id='my-id'></div>

Or you can define it in a stylesheet:

div#my-id { display:inline; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM