简体   繁体   English

通过jQuery直接定位下一个元素

[英]Target DIRECTLY next element by jQuery

Please help me to target DIRECTLY the next <section> after the element <section class="active"> 请帮助我将元素<section class="active">之后的下一个<section>直接定位为目标

Here is code. 这是代码。

    <main>
        <section id="video" class="active">
            <video src="1.mp4" autoplay loop></video>
        </section>
        <section id="video">
            <video src="2.mp4" muted autoplay loop></video>
        </section>
        <section id="video">
            <video src="3.mp4" muted autoplay loop></video>
        </section>
    </main> 

I've tried this one 我已经试过了

`$(this).next().find($("video").prop('muted', false));`

and this one 还有这个

`$(this).closest().find($("video").prop('muted', false));`

but both of them takes all <section> s and makes their videos unmuted. 但他们两个都使用了<section>并使视频保持静音。

I just want to unmute directly the next section's video which has class .active. 我只想直接取消下一节具有.active类的视频的静音。

You're chaining your calls slightly wrong. 您的通话链错误。 Try: 尝试:

 $(this).next().find('video').prop('muted', false);

To explain: 解释:

.next() -> (Returns the immediate sibling element) -> find('video') (Returns the 'video' element contained in this sibling) and finally -> .prop() (set the property value) .next()->(返回直接的同级元素)-> find('video')(返回此同级中包含的'video'元素),最后-> .prop()(设置属性值)

'Find' takes a selector as an argument, rather than a jquery object. “查找”将选择器作为参数,而不是jquery对象。

Also, it's bad to have multiple elements with the same 'ID' on a page. 同样,在页面上包含多个具有相同“ ID”的元素也是很糟糕的。 If you need to apply common styles, consider using classes instead. 如果需要应用通用样式,请考虑使用类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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