How do I select item with class within a DIV in JQ

2019-01-16 03:23发布

I have the following HTML:

<div id="mydiv"><div class="myclass"></div></div>

I want to be able to use a selector that selects the inside div, but specific for the "mydiv" container. How can I achieve this with JQuery?

6条回答
2楼-- · 2019-01-16 04:00

try this instead $(".video-divs.focused"). This works if you are looking for video-divs that are focused.

查看更多
做个烂人
3楼-- · 2019-01-16 04:07

More efficient and faster one is...

$('#mydiv').find('.myclass').html('<span>Hello World</span>');
查看更多
Viruses.
4楼-- · 2019-01-16 04:08

You'll do it the same way you would apply a css selector. For instanse you can do

$("#mydiv > .myclass")

or

$("#mydiv .myclass")

The last one will match every myclass inside myDiv, including myclass inside myclass.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-16 04:17

Try this

$("#mydiv div.myclass")
查看更多
Explosion°爆炸
6楼-- · 2019-01-16 04:19

Try:

$('#mydiv').find('.myclass');

JS Fiddle demo.

Or:

$('.myclass','#mydiv');

JS Fiddle demo.

Or:

$('#mydiv .myclass');

JS Fiddle demo.

References:

查看更多
我命由我不由天
7楼-- · 2019-01-16 04:21

If you want to select every element that has class attribute "myclass" use

$('#mydiv .myclass');

If you want to select only div elements that has class attribute "myclass" use

$("div#mydiv div.myclass");

find more about jquery selectors refer these articles

查看更多
登录 后发表回答