【问题标题】:how to find image inside a div using jquery如何使用jquery在div中查找图像
【发布时间】:2018-05-02 07:31:43
【问题描述】:

我正在处理一个 html 页面,其中有 div 元素和内部 div 元素。在内部 div 元素中有一个图像元素。主 div 和内部 div 的数量为 2。

<div class="owl-stage-outer">

<div class="owl-item" style="width: 1903px;"><div class="header-single-slider">
                    <figure>
                        <img src="assets/img/sliders/slider01.jpg" alt="">
                        <figcaption>
                            <div class="content">
                                <div class="container inner-content text-left">
                                    <h1 style="display: block;" class="fadeInUp animated">We Build Your<br><span>Business</span> IDEA</h1>
                                    <p style="display: block;" class="fadeInDown animated">There are many variations of passages of Lorem Ipsum available but the majority have suffered injected humour dummy now.</p>
                                    <a href="#" class="boxed-btn fadeInDown animated" style="display: inline-block;">Read More <i class="fas fa-arrow-right"></i></a>
                                </div>
                            </div>
                        </figcaption>
                    </figure>
                </div></div>

 <div class="owl-item active" style="width: 1903px;"><div class="header-single-slider">
                    <figure>
                        <img src="assets/img/sliders/slider02.jpg" alt="">
                        <figcaption>
                            <div class="content">
                                <div class="container inner-content text-left">
                                    <h1 style="display: block;" class="fadeInUp animated">We Build Your<br><span>Business</span> IDEA</h1>
                                    <p style="display: block;" class="fadeInDown animated">There are many variations of passages of Lorem Ipsum available but the majority have suffered injected humour dummy now.</p>
                                    <a href="#" class="boxed-btn fadeInDown animated" style="display: inline-block;">Read More <i class="fas fa-arrow-right"></i></a>
                                </div>
                            </div>
                        </figcaption>
                    </figure>
                </div></div>
</div>

这里,class为'owl-item'的main div被使用了2次,class为'header-single-slider'的inner div也被使用了2次。当程序运行类为'owl-item'的主div时,其类变为'owl-item acitve'意味着该类现在处于活动状态,第二个div类保持为'owl-item'。几秒钟后,第二个 div 将变为活动状态,其类变为“owl-item active”,第一个将保持非活动状态,其类将为“owl-item”

我想找到类为“owl-item active”的那个 div 的图像 src。为此,我使用了以下查询:

alert($('.owl-item active').children('div').children("img").attr('src'));

但它显示警报消息为“未定义”。如何解决?

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    你没有正确选择你的activeowl-item

    你可以简单地使用

    $('.owl-item.active>div>figure>img').attr('src')
    

    如果您确定您的 div 中只有一个 img,请改用 '.owl-item.active img'

    console.log($('.owl-item.active&gt;div&gt;figure&gt;img').attr('src'));
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="owl-stage-outer">
    
      <div class="owl-item" style="width: 1903px;">
        <div class="header-single-slider">
          <figure>
            <img src="assets/img/sliders/slider01.jpg" alt="">
            <figcaption>
              <div class="content">
                <div class="container inner-content text-left">
                  <h1 style="display: block;" class="fadeInUp animated">We Build Your<br><span>Business</span> IDEA</h1>
                  <p style="display: block;" class="fadeInDown animated">There are many variations of passages of Lorem Ipsum available but the majority have suffered injected humour dummy now.</p>
                  <a href="#" class="boxed-btn fadeInDown animated" style="display: inline-block;">Read More <i class="fas fa-arrow-right"></i></a>
                </div>
              </div>
            </figcaption>
          </figure>
        </div>
      </div>
    
      <div class="owl-item active" style="width: 1903px;">
        <div class="header-single-slider">
          <figure>
            <img src="assets/img/sliders/slider02.jpg" alt="">
            <figcaption>
              <div class="content">
                <div class="container inner-content text-left">
                  <h1 style="display: block;" class="fadeInUp animated">We Build Your<br><span>Business</span> IDEA</h1>
                  <p style="display: block;" class="fadeInDown animated">There are many variations of passages of Lorem Ipsum available but the majority have suffered injected humour dummy now.</p>
                  <a href="#" class="boxed-btn fadeInDown animated" style="display: inline-block;">Read More <i class="fas fa-arrow-right"></i></a>
                </div>
              </div>
            </figcaption>
          </figure>
        </div>
      </div>
    </div>

    【讨论】:

    • 你能帮我看看图片src是包含1,2还是3。
    • @SunnySandeep 再问一个问题并链接给我,我去看看。
    • 我不知道如何链接。
    • @SunnySandeep 看我的评论
    • 在问另一个问题之前,先在 StackOverflow 上寻找答案,你的问题已经被问过很多次了。答案应包括indexOfRegEx
    【解决方案2】:

    您在激活之前忘记了.

    $('.owl-item.active img').attr('src')
    

    【讨论】:

    • 你在owl-item之前忘记了. :D
    • 你能帮我看看图片src是包含1,2还是3。
    • @SunnySandeep var src= $('.owl-item.active img').attr('src') if(src.indexOf("1") != -1)
    • @SunnySandeep 告诉我是否有帮助
    • 如果 src=1 或 2 或 3 OR 包含,请解释你想要的
    【解决方案3】:

    您可以使用find 方法。

    console.log($('.owl-item.active').find('figure>img').attr('src'));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      相关资源
      最近更新 更多