简体   繁体   English

Jquery - 将click事件绑定到变量

[英]Jquery - binding click event to a variable

All, 所有,

I am really stuck/ confused at this point. 在这一点上我真的很困惑/困惑。

I have an array with 6 items in it. 我有一个包含6个项目的数组。 Each item in the array is dynamically filled with elements using jquery '.html' method. 数组中的每个项都使用jquery'.html'方法动态填充元素。 However, I cannot seem to be able to attach/ bind an event to this dynamically created variable. 但是,我似乎无法将事件附加/绑定到此动态创建的变量。

As soon as the browser gets to the problem line (see the area labeled 'PROBLEM AREA'), I get a 'undefined' error, which is really confusing as all the previous code on the very same variable works just fine. 一旦浏览器到达问题行(参见标记为“问题区域”的区域),我就会收到“未定义”错误,这实际上令人困惑,因为同一个变量上的所有先前代码都可以正常工作。

var eCreditSystem = document.getElementById("creditSystem");
var i = 0;
var eCreditT = new Array(6);                    // 6 members created which will be recycled

function createCreditTransaction ()                 // func called when a transaction occurs, at the mo, attached to onclick()
{
    if (i < 6)
    {
        eCreditT[i] = undefined;                    // to delete the existing data in the index of array
        addElements (i);
    } else
    if (i > 5 || eCreditT[i] != undefined)
    {
         ...
    }
}

function addElements (arrayIndex)                   // func called from within the 'createCreditTransaction()' func
{
    eCreditT[i] = $(document.createElement('div')).addClass("cCreditTransaction").appendTo(eCreditSystem);
    $(eCreditT[i]).attr ('id', ('trans' + i));
    $(eCreditT[i]).html ('<div class="cCreditContainer"><span class="cCreditsNo">-50</span>&nbsp;<img class="cCurrency" src="" alt="" /></div><span class="cCloseMsg">Click box to close.</span><div class="dots"></div><div class="dots"></div><div class="dots"></div>');
    creditTransactionSlideOut (eCreditT[i], 666);                   // calling slideOut animation
    console.log(eCreditT[i]);     // this confirms that the variable is not undefined

/* ***** THE PROBLEM AREA ***** */
    $(eCreditT[i]).on ('click', function ()                 // if user clicks on the transaction box
    {
        creditTransactionSlideBackIn (eCreditT[i], 150);                    // slide back in animation
    });
    return i++;
}

Try this: 试试这个:

$(eCreditT[i]).bind('click', function() {
   creditTransactionSlideBackIn(eCreditT[i], 150);
});

Edit: use ++i instead of i++ like this: 编辑:使用++i代替i++如下所示:

return ++i;
/*
or
i += 1;
return i;
*/

retrurn ++i performs the increment first then return i after the increment. retrurn ++i首先执行增量,然后在增量后返回i While return i++ return i then icrement it. 虽然return i++返回i然后icrement它。

Nonsense create an element using JavaScript and then use jQuery function to transform it into a jQuery object. 废话使用JavaScript创建一个元素,然后使用jQuery函数将其转换为jQuery对象。 You can let jQuery create the element directly for you. 您可以让jQuery直接为您创建元素。

eCreditT[i] = $('<div>').addClass("cCreditTransaction").appendTo(eCreditSystem);

Also, since eCretitT[i] is already a jQuery element, no need to call the jQuery function again. 此外,由于eCretitT [i]已经是一个jQuery元素,因此无需再次调用jQuery函数。

eCreditT[i].on('click', function () {
    creditTransactionSlideBackIn(eCreditT[i], 150);
});

If you already tried on , bind , live and click methods, then maybe the called function is your problem. 如果你已经尝试过onbindliveclick方法,那么也许被调用的函数是你的问题。 Try to put a console.log() or an alert() inside the function to make sure the click event is actually happening. 尝试在函数内部放置console.log()alert()以确保实际发生click事件。 If it happens then the function creditTransactionSlideBackIn() is your problem. 如果它发生,那么函数creditTransactionSlideBackIn()是你的问题。

EDIT 编辑

The problem is when the event takes place, i is not the original variable anymore. 问题是当事件发生时, i不再是原始变量了。

function addElements (arrayIndex)
{
    eCreditT[i] = $('<div>').addClass("cCreditTransaction").appendTo(eCreditSystem);
    eCreditT[i].attr ('id', ('trans' + i));

    eCreditT[i].data ('id', i);    // Store the id value to a data attribute

Then when you call the function you can refer to the data attribute instead of the i variable: 然后,当您调用该函数时,您可以引用data属性而不是i变量:

/* ***** THE PROBLEM AREA ***** */
    eCreditT[i].on ('click', function ()                 // if user clicks on the transaction box
    {
        creditTransactionSlideBackIn ($(this).data('id'), 150);                    // slide back in animation
    });
    return i++;
}

try to bind parent div and then use if($e.target).is('some')){} , it will act as .live, like this: 尝试绑定父div然后使用if($e.target).is('some')){} ,它将充当.live,如下所示:

$(eCreditSystem).bind('click', function (e){
    if($(e.target).is('.cCreditTransaction')) {
        creditTransactionSlideBackIn ($(e.target), 150);              
    }
});

of course you'll need in a minute larger if for checking if clicked dom el is a child of .cCreditTransaction , like this: 当然,你需要在一分钟内较大的if检查,如果点击的DOM EL是一个孩子.cCreditTransaction ,就像这样:

if($(e.target).parents().is('.cCreditTransaction')){}

尝试从addElements()函数中添加click事件并尝试一次。

Try this: 试试这个:

$(eCreditT[i]).live('click', function() {
   creditTransactionSlideBackIn(eCreditT[i], 150);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM