gpt4 book ai didi

javascript - 如何使用 jQuery 从
  • 元素内部获取值并将其放入图像属性标记
  • 转载 作者:塔克拉玛干 更新时间:2023-11-02 21:41:29 27 4
    gpt4 key购买 nike

    我想从 H3 元素中获取值并将其放入图像属性中,例如 title 和 alt。请在下面查看我的代码。

    $('ul.products').each(function() {
    $(this).find('li h3').each(function() {
    var current = $(this);
    if (current.children().size() > 0) {
    return true;
    }
    console.log($(this).text());
    });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <ul class="products">
    <li>
    <a href="#">
    <img src="/path/" title="(Get value from h3)" alt="(Get value from h3)">
    </a>
    <h3>Mytitle1</h3>
    </li>
    <li>
    <a href="#">
    <img src="/path/" title="(Get value from h3)" alt="(Get value from h3)">
    </a>
    <h3>Mytitle2</h3>
    </li>
    <li>
    <a href="#">
    <img src="/path/" title="(Get value from h3)" alt="(Get value from h3)">
    </a>
    <h3>Mytitle3</h3>
    </li>
    </ul>

    最佳答案

    $(function() {
    $('.products li').each(function() {
    var item = $(this);
    var text = item.find('h3').text();

    item.find('img').attr({
    'title': text,
    'alt': text
    });
    });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <ul class="products">
    <li>
    <a href="#"><img src="/path/" title="(Get value from h3)" alt="(Get value from h3)"> </a>
    <h3>Mytitle1</h3>
    </li>
    <li>
    <a href="#"><img src="/path/" title="(Get value from h3)" alt="(Get value from h3)"> </a>
    <h3>Mytitle2</h3>
    </li>
    <li>
    <a href="#"><img src="/path/" title="(Get value from h3)" alt="(Get value from h3)"> </a>
    <h3>Mytitle3</h3>
    </li>
    </ul>

    关于javascript - 如何使用 jQuery 从 <li> 元素内部获取值并将其放入图像属性标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560539/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com