开发者

JQuery, a new Selection using the results

开发者 https://www.devze.com 2023-01-29 03:37 出处:网络
Is there a way to me do this? <img id=\"example\" src=\"anything.jpg\" title=\"something\" class=\"abc\" />

Is there a way to me do this?

<img id="example" src="anything.jpg" title="something" class="abc" />

$('.abc').each(function(){

   //test if this result is something
   if( $(this)...(???)...('[src^=anything]')) == 'anything.jpg'){

   }

   //another Jquery selector test for this one    
   if( $(this)...(???)...('#example').size() > 0){

   }


});

This is just an example, what I need is pretty more complex.. But I would like to know if there is a way to make other jQuery selector test in the result of a first selector.. since "find" will find the children of $(this).. and .parent() get alot of brothers..

See what I mean? Do you have any idea?



So sorry.. let me try again..

$('div').each();

get all "div", right?

But now in that function I need to make another "test" check if div class is "red" or "blue"..

See?

I need to开发者_Go百科 test something else of the result based in Jquery selector..

I know I could do:

class = $(this).attr('class'); and then if(class=="blue"){} .. But I would like to do $('this[class=blue]').size()>0){}


The jQuery is() filter operates on a found set to detect if something is true or not.

The jQuery filter() method will further pare down a found set based on criteria.

var allDivs   = $('div');
var greenOnes = allDivs.filter('.green');
var redOnes   = allDivs.filter('.red'  );


I think you need the is method:

$('.abc').each(function() {
    $(this).is('[src^=anything]')
});

This is fairly simple though, but I can't really tell what you are trying to do by the description. Maybe this is enough to get you started though.


You can use the filter and is methods to filter/search within a jQuery object.


if( $(this).is('[src^="anything"]') ) {
}
elseif( $("#example").size() > 0) {
}

You could put $("#example") in a variable outside of the loop and then reference it inside the loop as well.


if(this.src.indexOf("anything") === 0) {
    // source starts with 'anything'
}

if($("#example").length) {
    // since there can be only one #example
    // in a *valid* document
}

Based on your edit:

if($(this).hasClass("blue")) {
    ...
}

?

0

精彩评论

暂无评论...
验证码 换一张
取 消