同时追加和绑定的jquery问题 [英] Jquery problem with appending and binding in the same time

查看:138
本文介绍了同时追加和绑定的jquery问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的网站设计一个精美的滚动选择解决方案。

我做了一个< a name =iwanthereanidclass =popper>点击这里!< / a> link。

通过点击它,我调用了一个 popup()函数。

  $(。popper)。click(function(){
popup($(this));
});

popup()这已经打开了吗?否?

让我们制作一个div,并用 fillwiththings()来填充它。

<$
{
if(where.hasClass(on))
{
where.removeClass(on );
where.next(div)。remove();
}
else
{
where.addClass(on);
fillwiththings(where);



函数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();


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>')。




$ fillwiththings()
$> / code>我想通过where参数绑定到附加的< a>< / a> 另一个函数,关闭并替换a(Click Here!)标签的文本和名称值。但绑定失败。我错过了什么?请帮忙。



谢谢。

解决方案

无法正确访问其中参数。事件数据不作为参数传递给事件处理函数,它是事件对象的属性:

  function selectclose(event ){
event.data.where
.attr(name,$(this).attr(name))
.text($(this).text())
.removeClass(on)
.next(div)。remove();
}

另一个问题是您传递返回值 selectclose()来绑定。您必须直接传递函数:

$ p $ $ container.append($(...)。bind(click, {where:where},selectclose));
//无括号------ ^


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?
Lets make a div and fill it with things by 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();
}

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 ------^

这篇关于同时追加和绑定的jquery问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆