Create a variable in jquery with html content

前端 未结 5 1008
死守一世寂寞
死守一世寂寞 2021-02-09 13:18

Hi I am trying to create a variable in jquery that contains a table for outputting in different areas of a website. But it is giving me an error, and I do not understand why. He

相关标签:
5条回答
  • 2021-02-09 13:48

    Syntax error due to wrongly assigned string.

    concatenate the lines

    var copy = "<table width='750' border='0' cellspacing='0' cellpadding='0'>" 
                + "<tr>";
      ....
    
    0 讨论(0)
  • 2021-02-09 13:50

    You haven't handled the line returns in your string. Because of this, javascript assumes that the end of each line is the end of a statement. Clearly each line is not a valid statement. Concatenate your string like so:

    var "multi-"+
        "line "+
        "string";
    
    0 讨论(0)
  • 2021-02-09 13:53

    Or you can use this tool http://www.accessify.com/tools-and-wizards/developer-tools/html-javascript-convertor/

    It works fine :)

    0 讨论(0)
  • 2021-02-09 14:03

    When I have complex html this is what I do. I put the html in an enclosing DIV and get the html content

    var copy = $('#mycomplexhtml').html(); //gets the content I placed in an hidden div
    
    
    <!-- I normally place this at the bottom-most part of the page -->
    <div id="mycomplexhtml" style="display:none">
      <table width='750' border='0' cellspacing='0' cellpadding='0'>
      <tr>
        <td>Tarifa valida desde:</td>
        <td>Tarifa valida hasta:</td>
        <td>Tarifa MXN</td>
        <td>Tarifa USD</td>
      </tr>
     ...
      </table>
    </div>
    
    0 讨论(0)
  • 2021-02-09 14:06

    You could concatenate strings like it was suggested. Or another way is to escape new line characters with back slash:

    var html = "<table> \
        <tr>....</tr> \
        </table>";
    
    0 讨论(0)
提交回复
热议问题