【发布时间】:2015-08-23 22:06:51
【问题描述】:
这是我试图复制的 div
{%for i in current_user.posts%}
<div class = "own_posts" id = "{{'id_' + i.id|string}}">
<img src = "{{url_for('static',filename='user_prof_pic/' + current_user.prof_pic)}}">
<a href = "{{url_for('main.ProfilePage',user = current_user.first_name)}}">{{current_user.first_name}}</a>
<p>{{i.post}}</p>
<div class ="like_comment">
<ul>
<li><a href="#">like</a></li>
<li><a href="#">comment</a></li>
</ul>
</div>
<form class ="comment_form">
<input type="text" action = "#" name = "comment_box" class ="comment_box" id = "{{'id_' +i.id|string}}">
<input type="submit" value="comment" class ="submit" id = "{{'id_' + i.id|string}}">
</form>
</div>
{%endfor%}
到目前为止,我已经成功地使用 jquery 预先添加它并更改新添加的 div 的 ID。
var count = 4
$(".submit").click(function(event){
event.preventDefault()
var id = $(this).attr("id")
var comment = $(".comment_box#" + id).val()
console.log(count)
count++
$.ajax({
type:"POST",
url:"#",//"{{url_for('main.ProfilePage',user = current_user.first_name)}}",
data: JSON.stringify({"comments":comment}),
contentType:"application/json;charset=UTF-8"
});
var div_copy = $(".own_posts#id_2").clone()
var div_copy2 = div_copy.attr("id","id_" + count)
$("#own_stream").prepend(div_copy2)
});
但是,前置 div 中的 ID 表单仍然包含克隆它的 div 的 ID。澄清
var div_copy = $(".own_posts#id_2").clone() 这个 div 中的表单包含 id_2 的 id,所以新添加的 div 的表单 id 仍然是 id_2
我更改了前置 div 的 ID 这样做:
var div_copy = $(".own_posts#id_2").clone()
var div_copy2 = div_copy.attr("id","id_" + count)
$("#own_stream").prepend(div_copy2)
但我不知道如何访问这个新克隆的 div 中的表单并将其更改为 ID。
我们如何做到这一点?
我这样做对吗?我正在尝试学习 Web 开发,但不想了解 facebook、twitter 等网站如何在不刷新页面的情况下将您新发布的状态/推文显示到页面中。
我在做什么是如何工作的?如果不 对新手有所了解
这只是一个实践概念的测试
【问题讨论】:
-
ID 是开始学习如何使用 jQuery 的最简单的选择器......但对于这种情况,它们比使用普通类和遍历更复杂。例如,在您的每个
own-posts容器中,一个简单的find()将隔离仅存在于该容器实例中的任何内容。 -
@charlietfl 你能写下一个例子作为答案让我接受吗?
标签: javascript jquery html python-3.x flask