spread html in multiple lines javascript

前端 未结 4 1686
轻奢々
轻奢々 2021-02-05 21:09

I want to put the below html code in my javascript function. I don\'t want to put it all next to each other. Is it possible to the code in the same way as how it is in html? Cod

4条回答
  •  无人共我
    2021-02-05 21:26

    You can kind of do this:

    var myHtml = [
     'firstline',
     'second line',
     'third line'].join("\n");
    

    So pretty much you still need to break up the code some how.

    An alternative (if the code is going to be large) would be to store in a HTML file and when you need, retrieve it quickly with $.get and possibly cache it somewhere in a variable.

    $.get('snippet.html', function(data) {
      $('.result').html(data);
    });
    

    I think this is actually a pretty good idea as modifying that snippet will be much easier.

提交回复
热议问题