Add a “new line” in innerHTML

前端 未结 3 1994
猫巷女王i
猫巷女王i 2020-12-13 19:07

I am trying to create a table with images in first cell and information about the pic in second cell.

I need to add different information in one cell, like that:

相关标签:
3条回答
  • 2020-12-13 19:16

    The simplest way is by adding a line break as html

    cellTwo.innerHTML = arr_title[element] + "<br />" + arr_tags[element];
    

    If you want your newlines to be treated literally, you could use the <pre> tag

    cellTwo.innerHTML = 
        "<pre>" + arr_title[element] + "\n" + arr_tags[element] + "</pre>";
    
    0 讨论(0)
  • 2020-12-13 19:19

    No, <br /> does not work in asp .net but you can instead write it like so

    cellTwo.innerHTML = arr_title[element] + arr_tags[element]; arr_title[element] + "/n" + arr_tags[element];
    

    Edit - alternative wrapped in code tags

    cellTwo.innerHTML = arr_title[element] + arr_tags[element];
    cellTwo.innerHTML += arr_title[element] + "/n" + arr_tags[element];
    

    Semicolon ";" seems to act as line breaks Remember the "+=" to assign multiple values to the string

    0 讨论(0)
  • 2020-12-13 19:28

    To round out your understanding:

    Since it is html (innerHTML) it renders html and you can use any html you wish, so in this case simply add an good old fashioned <br>:

    var test = document.getElementById('someElementId');
    test.innerHTML = "The answer <br>to life, the universe, and everything...<br> is 42.";
    

    If it were a string, such as in an alert box or text box etc. then /n would be correct:

    alert('Never /n Forget your towel.'); 
    

    Happy Coding!
       - $cr1ptN!nj@

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