在函数jquery中附加图像

Append image in function jquery

本文关键字:图像 函数 jquery      更新时间:2023-09-26

晚上好。我有这个jquery代码,它允许我在按下Enter键后发布评论。

Fattio说,我运行了一个带有用户名和用户想要发布的内容的append。

除了用户名,我还想使用他们的路径"挂起"个人资料图片。如何发布照片?

谢谢你的帮助。这是代码:

function commento_post(id_post)
{
    $('.form-commento'+id_post).click(function ()
    {
        $('#ins_commento'+id_post).keydown(function (e)
        {
            var message = $("#commento"+id_post).val();
            var username = $("#username").val();
            var id_user = $("#id_user").val();
            if(e.keyCode === 13)
            {
                $.ajax(
                {
                    type: 'POST',
                    url: 'http://localhost/laravel/public/index.php/post/ins_commento',
                    data: { commento: message, id_post: id_post },
                    success: function(data) 
                    {
                        $('#commento'+id_post).val('');
                        $('#commentscontainer'+id_post).append
                        (
                            /*$(".username-commento"+id_post).html
                            (*/
                                $('<a/>', 
                                { 
                                    text : username, href : 'http://localhost/laravel/public/index.php/utente/'+id_user
                                }).css({'font-weight':'bold'})
                            //)
                        ).append(' ').append(message).append($('<br/>'));
                        var el = $('#numero_commenti'+id_post);
                        var num = parseInt(el.text());
                        el.text(num + 1);
                    }
                });
            }
        });
    });
}

在您的成功函数中,您可以通过以下方式大大简化所有内容,而不需要太多使用jQuery append,只需使用一个变量来存储代码,然后一次性将其追加。这将允许您附加所有类型的元素,它很容易为您解析,并减少了您必须进行的调用量。

// Add whatever you want your final HTML to look like to this variable
var html = "<a href='http://localhost/laravel/public/index.php/utente/" + id_user + "' style='font-weight: bold;'>" + username + "</a>";
    html += message;
    // add an image
    html += "<img src='path/to/image.jpg' />"
    html += "<br />";
// append to code you constructed above in one go
$('#commentscontainer' + id_post).append(html);

更新

我修改了一个不正确的引用,并将+ id_user + "更改为+ id_user + "',这使它之后的一切都正常工作。