English 中文(简体)
Javascript - Jquery, 约束动态的选任人,使用手法(不是扼制)
原标题:Javascript - Jquery, bind a dynamic selector with On method (not a string)

我想利用“On”功能附上一个事件,因为其他人被贬损(生命、代表、click......)。

但是,问题是:如果我们产生目标,我们需要在参数中使用一位选择,而这一参数就是一个str!

Sample : (context : dynamic table)

//Wrong way

$("#dataTable tbody tr").on("click", function(event){
    alert($(this).text());
});

//Good way

$("#dataTable tbody").on("click", "tr", function(event){
    alert($(this).text());
});

现在,如果我想要找到办法避免这种情况,我怎么做。

    // ?? (find div)
    $("#dataTable tbody").on("click", "tr > ul > li > ul > li > div", function(event){
            alert($(this).text());
        });

   // What I would like to do
    $("#dataTable tbody").on("click", $(this).find("div"), function(event){
            alert($(this).text());
        });

   //and I don t want to do this :
    $("#dataTable tbody").find( div ).on("click", function(event){
            alert($(this).text());
        });

Thank you !

最佳回答

working/em> 等值:

// What I would like to do
$("#dataTable tbody").on("click", $(this).find("div"), function(event){
    alert($(this).text());
});

is...

// What I would like to do
$("#dataTable tbody").on("click",  div , function(event){
    alert($(this).text());
});

If you to target div s inside the nested list, then you might be able to get away with:

// What I would like to do
$("#dataTable tbody").on("click",  ul ul div , function(event){
    alert($(this).text());
});

... but it depends if you have other ul s in your tbody; you only need to be specific enough in your selector to remove the other elements from consideration. Very rarely would you need to be as specific as tr > ul > li > ul > li > div, and in that case it d be best to apply a class to an element nearer the div (or the div itself) and select around that instead.

Also note that it is only live() that is depreciated. At the time of writing (1.7.2) delegate() is not, although I believe it will be in 1.8.

There is no talk of depreciating click() or it s shortcut counterparts.

问题回答

If you want to select DIV elements which are inside nested lists, then this would be one way to do it:

$(  #foo  ).on(  click ,  ul ul div , function () { 

However, I recommend classes. Just set a class on your DIV elements and then:

$(  #foo  ).on(  click ,  .bar , function () { 




相关问题
Mysql trigger/events vs Cronjob

I have an auction website which let my users place an unlimited amount of autobiddings. To monitor these autobiddings something has to check the database every second. My question is if it is ...

Can an event be used as an event listener?

I m trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this: private WindowUpdateServer ...

鸡奸

由于将javascript DOM方法放在第html页底部(在<有人>之后)远比利用“j Query”准备活动要快得多,因此我们不得不这样做:

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

jQuery bind on ajax load() event

I have a page which displays multiple blocks with results details. Inside each block I have some <a> tags with thickbox jQuery plugin attached: class="thickbox" Here is an example of one kind ...

HTML text input event

I have a form, and want to disable/enable its submit button depending on whether the form s input text fields are empty or have had text entered into them. I think this means having an event handler ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...

热门标签