How To Append (Or Other Method) a Lot of HTML Code

2020-06-16 05:22发布

I need to append a lot of HTML code. To improve readability, I don't want to write that all in one line, but split them as regular HTML. That would be like 15 new-lines or something. The problem is that JavaScript doesn't allow me to do that.

var target = $('.post .comment_this[rel="' + comment_id + '"]').parent().parent();

target.append('
    <div class="replies">
        <p>...</p>
        <img src="" alt="" />
    </div>
');

Basically, I need to add HTML in that place.

I need to add some variables too.

6条回答
\"骚年 ilove
3楼-- · 2020-06-16 05:52

If you want to insert variables to html, you can use some templating library like jQuery.template

查看更多
beautiful°
4楼-- · 2020-06-16 06:02
    target.append(' <div class="replies">'+
            '<p>...</p>'+
            '<img src="" alt="" />'+
        '</div>'
    );

or

    target.append(' <div class="replies">\
            <p>...</p>\
            <img src="" alt="" />\
        </div>'
    );
查看更多
该账号已被封号
5楼-- · 2020-06-16 06:05
target.append(' ?>
    <div class="replies">
        <p>...</p>
        <img src="" alt="" />
    </div>
<?');

Separate the html and php with the close/open php tags and it should work fine.. when adding var's in the html, just use the tags again, like this: <? $hello ?>

查看更多
你好瞎i
6楼-- · 2020-06-16 06:14

use html() instead of append

target.html('<div class="replies"><p>...</p><img src="" alt="" />,</div>');
查看更多
叛逆
7楼-- · 2020-06-16 06:17

As of 2015, ECMA 6, you can do this:

target.append(`
    <div class="replies">
        <p>...</p>
        <img src="" alt="" />
    </div>
`);
查看更多
登录 后发表回答