亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Bootstrap 4 更改下拉菜單中的圖像值

使用 Bootstrap 4 更改下拉菜單中的圖像值

慕蓋茨4494581 2021-11-18 15:52:03
我想在下拉值更改時更改圖像的值,而不僅僅是文本。我該怎么做?HTML<div class="input-group">    <div class="input-group-btn">        <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown">            <img src="https://i.postimg.cc/44bmkyGL/sdgs-1.jpg"> One        </button>        <div class="dropdown-menu">            <a class="dropdown-item sectionScroll" href="#one"><img src="https://i.postimg.cc/44bmkyGL/sdgs-1.jpg"> One</a>            <a class="dropdown-item sectionScroll" href="#two"> <img src="https://i.postimg.cc/4NWyy0mp/sdgs-2.jpg"> Two</a>            <a class="dropdown-item sectionScroll" href="#three"> <img src="https://i.postimg.cc/dVD1y9yp/sdgs-3.jpg"> Three</a>            <a class="dropdown-item sectionScroll" href="#three"> <img src="https://i.postimg.cc/pdFL0XkB/sdgs-4.jpg"> Four</a>            <a class="dropdown-item sectionScroll" href="#three"> <img src="https://i.postimg.cc/85gPxQ0g/sdgs-5.jpg"> Five</a>        </div>    </div></div>JS:$(".dropdown-menu a ").click(function(){  $(this).parents(".input-group-btn").find('.btn').text($(this).text    ());});
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

試試這個 usingclosest()方法來找到正確的元素。


$(".dropdown-menu a ").click(function() {

  $image = $(this).find('img').attr("src")

  $(this).closest('.input-group-btn').find('button img').attr("src", $image);

  $(this).closest('.input-group-btn').find('button span').text($(this).text());

});

img {

  width: 24px;

}

<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js" integrity="sha384-xrRywqdh3PHs8keKZN+8zzc5TX0GRTLCcmivcbNJWm2rs5C8PRhcEn3czEjhAO9o" crossorigin="anonymous"></script>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div class="input-group">

  <div class="input-group-btn">

    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown">

            <img src="https://i.postimg.cc/44bmkyGL/sdgs-1.jpg"> <span>One</span>

        </button>

    <div class="dropdown-menu">

      <a class="dropdown-item sectionScroll" href="#one"><img src="https://i.postimg.cc/44bmkyGL/sdgs-1.jpg"> One</a>

      <a class="dropdown-item sectionScroll" href="#two"> <img src="https://i.postimg.cc/4NWyy0mp/sdgs-2.jpg"> Two</a>

      <a class="dropdown-item sectionScroll" href="#three"> <img src="https://i.postimg.cc/dVD1y9yp/sdgs-3.jpg"> Three</a>

      <a class="dropdown-item sectionScroll" href="#three"> <img src="https://i.postimg.cc/pdFL0XkB/sdgs-4.jpg"> Four</a>

      <a class="dropdown-item sectionScroll" href="#three"> <img src="https://i.postimg.cc/85gPxQ0g/sdgs-5.jpg"> Five</a>

    </div>

  </div>

</div>


查看完整回答
反對 回復 2021-11-18
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

你可以試試這個:


 $(".dropdown-menu a ").click(function(){

  $image=$(this).children('img').attr("src")

  $(this).parents(".input-group-btn").find('.btn').children('img').attr("src",$image);

$(this).parents(".input-group-btn").find('.btn').text($(this).text()); });


查看完整回答
反對 回復 2021-11-18
  • 2 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號