Jquery problem with appending and binding in the same time
I'm working on a fine rolldown select solution for my website.
I made an<a name="iwanthereanid" class="popper">Click Here!</a>
link.
By clicking on it I call a popup()
function.
$(".popper").click(function() {
popup($(this));
});
In the popup()
function i check was this already opened? No?
fillwiththings()
.
function popup(where)
{
if (where.hasClass("on"))
{
where.removeClass("on");
where.next("div").remove();
}
else
{
where.addClass("on");
fillwiththings(where);
}
}
function selectclose(where)
{
var nameval = $(this).attr("name");
var textval = $(this).text();
where.attr("name", nameval);
where.text(textval);
where.removeClass("on");
where.next("div").remove();
开发者_StackOverflow社区}
function fillwiththings(where, id)
{
$container = $('<div class="popup"></div>')
for (i = 0; i < object.length; i++)
{
$container.append($('<a name="'+jsonobject[i]["id"]+'">'+jsonobject[i]["name"]+'</a>').bind("click", {where: where}, selectclose()));
}
}
In fillwiththings()
I'd like to bind to the appended <a></a>
an another function with the where parameter, to close, and replace the a(Click Here!) tag's text and name value. But the bind fails. What did i missed? Please help in this.
Thank you.
You are not accessing the where
parameter correctly. The event data is not passed as parameter to the event handler, it is a property of the event object:
function selectclose(event) {
event.data.where
.attr("name", $(this).attr("name"))
.text($(this).text())
.removeClass("on")
.next("div").remove();
}
The other problem is that you pass the return value of selectclose()
to bind. You have to pass the function directly:
$container.append($(...).bind("click", {where: where}, selectclose));
// no parenthesis ------^
精彩评论