Multi-line string insert using jQuery

前端 未结 4 1122
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 18:47
$(\"#addSelect\").click(function() {
        $(\"#optionsForm\").after(\"Hello world.\");
} );

This works.

$(\"#addSelect\").click(         


        
相关标签:
4条回答
  • 2020-11-27 19:22

    Change all the double quotes for the attributes to single quotes.

    $("#addSelect").click(function() { 
            $("#optionsForm").after("<tr> \
        <td><input type='text' class='optionField' value='Options' /></td> \
        <td> \
            <ul class='option'> \
                <li><select><option>Value..</option></select></li> \
            </ul> \
        </td> \
    </tr>"); 
    } ); 
    
    0 讨论(0)
  • 2020-11-27 19:35

    Use back ticks ` to start and finish the string

    $("#addSelect").click(function() {
        $("#optionsForm").after(`<tr>
        <td><input type="text" class="optionField" value="Options" /></td>
        <td>
            <ul class="option">
                <li><select><option>Value..</option></select></li>
            </ul>
        </td>
    </tr>`);
    } );

    0 讨论(0)
  • 2020-11-27 19:35

    I prefer to use something like this:

    $('<tr bgcolor="#ffffe6"><td></td><td colspan="8" id="orderNotes">'
        + inputNotes.val() + '</td> <td>' + todayStamp
        + '</td> </tr>')
    

    I feel like it's clean the + concatenation allows you to know we're working with the same black and it gives you the flexibility of adding variables.

    0 讨论(0)
  • 2020-11-27 19:46

    a cleaner approach is to use <script> tag

    https://stackoverflow.com/a/12097933/1416458

    <script id="stuff_you_want" type="text/plain">
    <tr>
        <td><input type="text" class="optionField" value="Options" /></td>
        <td>
            <ul class="option">
                <li><select><option>Value..</option></select></li>
            </ul>
        </td>
    </tr>
    </script>
    
    <script>
    
        // pure javascript
        var text = document.getElementById("stuff_you_want").innerHTML ;
    
        //or using jQuery... (document ready for safety)
        $(document).ready(function() {
    
            var text = $("#stuff_you_want").html(); 
    
        }
    
    </script>
    

    content-type must be set for html 4.0 compliance.

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