JQuery selector: select anchor tags that are descendants of a div with a specific identifier

Trying to choose the anchor tags that are descendants of a div with a specific identifier, for example, #mydiv1, #mydiv2and #mydiv3.

myFunction = function() {    
  var theDivs = $("#mydiv1, #mydiv2, #mydiv3");    
  theDivs.hover(function(e){    
      $(this+" a:link").css("color","#99ccff");
  },function(e){
      $(this+" a:link").css("color","#848484");        
  });    
}

The selector $(this+" a:link")seems to choose nothing.

Does anyone have any thoughts on the correct syntax for this choice?

+3
source share
5 answers

You can specify an element for the context that should work:

$("a:link", this).

It will search for anchors starting with thisnode.

+7
source

Try $(this).find("a:link")

EDIT: additional information

$(this + "query"), . jQuery selector . 'this' , . , - : $("." + this.className + "[query]")

+10

$(this).children("a:link")

http://api.jquery.com/children/

+1

, jQuery ": link". , , .

, , , "this" DOM, ​​ . , jQuery, "$ (this)" DOM .

0

Go through the div with .each () and use the child selector:

  myFunction = function() {    
      var theDivs = $("#mydiv1, #mydiv2, #mydiv3");    
  theDivs.each(function(){    
      $(this > 'a').css("color","#99ccff");
  });    
}

Same.

0
source

Source: https://habr.com/ru/post/1706988/


All Articles