在函数jquery中附加图像
Append image in function jquery
晚上好。我有这个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 + "'
,这使它之后的一切都正常工作。
相关文章:
- 如何通过函数调用设置图像的src
- 无法在java脚本中调用图像的点击事件函数
- 使用setInterval()函数进行图像闪烁
- 如何使用Javascript/Jquery/HTML5函数将HTML页面捕获为图像
- 如何创建背景图像列表'URL,并使用jQuery在单击函数时应用它们
- 带有内置图像对象的Canvas组件构造函数;t显示's图像
- 我可以将javascript绘制函数更改为图像吗
- 多次更改 onclick() 函数(播放/暂停图像更改)
- 如何从 JavaScript 中的函数传回图像
- 在多个元素上运行的函数插入多个图像
- JavaScript - 多参数函数,它是多个图像库的字符串
- 用于图像的 Javascript 函数
- JS函数不访问所有数组图像
- 单击图像更改图像并调用函数
- 如何使用JavaScript函数在HTML页面中正确设置背景图像
- jQuery:一旦加载了图像的子集,就执行函数
- 如何创建一个函数来根据需要加载图像
- 如何使用onmousemove函数在每次鼠标移动时更改背景图像
- 画布绘制图像函数在画布后插入 img
- Javascript显示图像函数:帮助简化函数