简体   繁体   中英

Jquery: call p element in parent function

I would like to select the p element in the following html

<div class="parent">
  <p>Stg</p>
</div>

in this JS

$('.parent').hover(
  function() {
    $(this.p).aFunction(){
  };
});

I can't find the correct selector...

There are a few ways to accomplish this:

find():

$(this).find('p').func();

children():

$(this).children('p').func();

Selector in context:

$('p', this).func();

Try:

$('.parent').hover(
  function() {
    $(this).find("p").someJQueryMethod();
});
$('.parent').hover(
  function() {
    $(this).find('p').aFunction(){
  };
});

You can use jquery children( ):

 $('.parent').hover( function() { console.log($(this).children("p").text()); }); 
 <div class="parent"> <p>Stg</p> </div> 

fiddle

使用选择器

$('.parent p')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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