简体   繁体   English

jQuery addClass() 到 append() 后生成的元素

[英]jQuery addClass() to element generated after append()

I am trying to add a class to a newly appended DIV without using something like:我正在尝试将一个类添加到新附加的 DIV 而不使用以下内容:

t.y.append('<div class="lol'+i+'"></div>');

Here's a better example of what I'm trying to do:这是我正在尝试做的一个更好的例子:

var t = this;

$(this.x).each(function(i, obj) {
    //append new div and add class too <div></div>
    t.y.append('<div></div>').addClass('lol'+i);
});

Page load HTML looks like:页面加载 HTML 如下所示:

<div class=".slideButton0 .slideButton1 .slideButton2" id="sliderNav">
    <div></div>
    <div></div>
    <div></div>
</div>

When you append an element through .append , it doesn't change the context of the jQuery object.当您通过.append附加元素时,它不会更改 jQuery 对象的上下文。

You could write it like this:你可以这样写:

$('<div></div>').appendTo(ty).addClass('lol'+i);

or要么

$('<div></div>').addClass('lol'+i).appendTo(ty);

(these both do the same thing, simply in different orders, the second possibly being more clear) (这些都做同样的事情,只是顺序不同,第二个可能更清楚)

the context of the jQuery object will be the newly created div. jQuery 对象的上下文将是新创建的 div。

t.y.append('<div></div>').addClass('lol'+i);

should be应该

t.y.append('<div></div>').find('div').addClass('lol'+i);

In the first case you are adding class to the div to which you are appending .. SO the context is still the parent div and not the newly appended div..在第一种情况下,您将类添加到要附加到的 div .. 所以上下文仍然是父 div而不是新附加的div ..

You need to find it first inside the parent and then add the class..您需要先在父级中找到它,然后添加类..

EDIT编辑

If you want to just add the class to the last appended element ... Find the last div in the parent and then add the class to it.. This will make sure you are not adding the class to all the div's every single time you iterate in the loop..如果您只想将类添加到最后一个附加元素......在父级中找到最后一个 div,然后将类添加到它......这将确保您不会每次都将类添加到所有 div在循环中迭代..

t.y.append('<div></div>').find('div:last').addClass('lol'+i);

Try this:试试这个:

tyappend($('<div></div>').addClass('lol'+i));

Fiddle: http://jsfiddle.net/gromer/QkTdq/小提琴: http : //jsfiddle.net/gromer/QkTdq/

var t = this;

$(this.x).each(function(i, obj) {
    //append new div and add class too <div></div>
    var d = $('<div />').addClass('lol' + i);
    t.y.append(d);
});

The problem is that append returns the container instead of the thing you just appended to it.问题是append返回容器而不是您刚刚附加到它的东西。 I would just do the addClass before the append instead of after:我只会在append之前而不是之后执行addClass

var t = this;

$(this.x).each(function(i, obj) {
    //append new div and add class too <div></div>
    t.y.append($('<div></div>').addClass('lol'+i));
});

EDIT ... or, in other words, exactly what Gromer said.编辑...或者,换句话说,正是格罗默所说的。 Beat me by five whole minutes, too.也比我整整打了五分钟。 I'm getting slow.我越来越慢了。

You don't mention why you want to number the class attribute to your list items, but in the case that you are actually using them for css don't forget you have :odd and :even css selector attritbutes and also the equivalent odd/even jQuery selectors.您没有提到为什么要将class属性编号到列表项,但是如果您实际上将它们用于 css,请不要忘记您有 :odd 和 :even css 选择器属性以及等效的odd/甚至 jQuery 选择器。

http://www.w3.org/Style/Examples/007/evenodd.en.html http://www.w3.org/Style/Examples/007/evenodd.en.html
http://api.jquery.com/odd-selector/ http://api.jquery.com/odd-selector/

I didn't find anything like this.我没有找到类似的东西。 notice the class attribute!注意类属性!

$.each(obj, function (_index, item) {
    resultContainer.append($('<li>', {
      class: "list-group-item",
      value: item.id,
      text: item.permitHolderName || item.permitHolderId
    }));
});

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

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