Reference an element of $(this) in jQuery

I'm iterating through a series of elements in jQuery (intentionally).

So basically I want something like this, but with the correct syntax:

$(".sonicrow").each(function() {
    $(this + 'somediv').css('background-color', 'red');
});

Obviously this is some kind of object/string mishmash. What would the correct syntax be for accessing the specific somediv within the this object?

Thanks, John.


$(".sonicrow").each(function() {
    $('somediv', this).css('background-color', 'red');
});

Where the second parameter is the "context" of the selector. Of cause your somediv have to be .somediv if it´sa class or #somediv if it´s an id.

This question is related to How to get the children of the $(this) selector? which also contains this answer

...
    $(this).find('somediv').css(...)
...

According to jQuery context selector $(selector, context) is implemented with $(context).find(selector) .


尝试这个:

$(".sonicrow").each(function() {
    $(this).find('somediv').css('background-color', 'red');
});

$(".sonicrow").each(function() {
    $(this).find('.somediv').css('background-color', 'red');
});

你可以这样做。

链接地址: http://www.djcxy.com/p/26692.html

上一篇: 垂直手风琴导航菜单

下一篇: 在jQuery中引用$(this)的元素