JQuery:选择动态创建的元素并推送到Firebase [英] JQuery: Selecting dynamically created elements and pushing to Firebase
问题描述
谢谢!
< pre $ approveRef.on('child_added',function(snapshot){
var posts = snapshot.val();
$('< div id = ($('#feed'))。text(posts.text).append('< button style =buttonid =approve> Approve< / button>')。appendTo ;
}); $($#
$('#approve')。on(click,function(){
var text = $('#post')。val();
postsRef .push({'text':text});
$('#post')。remove();
});
您必须绑定。 on()
放在一个已经在页面上的动态添加元素的容器中,并且像这样:
<$ p $('#c $ c> $('#yourContainer')。on('click','#approve',function(){
//你的代码在这里..
});
Beginner to all of this, playing around with Firebase. Basically, I want to retrieve text entries from Firebase and have an "Approve" button next to it. When the button is clicked, I want that specific text entry to be pushed to a new Firebase location and the text removed from the page. I am creating the button and the text dynamically and I am having some trouble with selecting the button and the divs I created. I know I have to use on() but I'm unsure of how to use it.
Thanks!
approveRef.on('child_added', function(snapshot) {
var posts = snapshot.val();
$('<div id="post">').text(posts.text).append('<button style ="button" id="approve">Approve</button>').appendTo($('#feed'));
});
$('#approve').on("click", function(){
var text = $('#post').val();
postsRef.push({'text':text});
$('#post').remove();
});
You have to bind .on()
on a container of your dynamically added element that is already on the page when you load it, and have it like this:
$('#yourContainer').on('click', '#approve', function(){
//your code here..
});
这篇关于JQuery:选择动态创建的元素并推送到Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!