简体   繁体   English

仅选择单击元素的“此”对象(而不选择同一类中的其他元素)

[英]Select 'this' object of clicked element only (not the other elements within the same class)

I am trying to change buttons text on click (using Bootstrap 4). 我试图更改单击时的按钮文本(使用Bootstrap 4)。 I have a problem with using the correct syntax. 我在使用正确的语法时遇到问题。

My html: 我的html:

<div class="btn-group">
  <button type="button" class="btn btn-outline-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="dropdownMenuButton2">
  Option 1                  
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton2">
    <a class="dropdown-item" href="#">Option 2</a>
    <a class="dropdown-item" href="#">Option 3</a>
 </div>
</div>

<div class="btn-group">
  <button type="button" class="btn btn-outline-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="dropdownMenuButton2">
  Type 1                  
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton3">
    <a class="dropdown-item" href="#">Type 2</a>
    <a class="dropdown-item" href="#">Type 3</a>
 </div>
</div>

and JS: 和JS:

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

  $(".btn:first-child").html($(this).text());

});

To rephrase - when I click on one button and change its value, the other one changes too... 换个说法-当我单击一个按钮并更改其值时,另一个按钮也会更改...

So I would like to change this value of the only clicked. 因此,我想更改此唯一点击的值。

You need to change the html of the .btn relative to your link. 您需要相对于您的链接更改.btnhtml

You can do that by selecting the closest .btn-group to your link, then searching for .btn inside it : 您可以通过选择最接近链接的.btn-group ,然后在其中搜索.btn来实现:

$(".dropdown-menu a").click(function () {
  $(this).closest('.btn-group').find('.btn').html($(this).text());
});

 $(".dropdown-menu a").click(function() { $(this).closest('.btn-group').find('.btn').html($(this).text()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="btn-group"> <button type="button" class="btn btn-outline-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="dropdownMenuButton2"> Option 1 </button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton2"> <a class="dropdown-item" href="#">Option 2</a> <a class="dropdown-item" href="#">Option 3</a> </div> </div> <div class="btn-group"> <button type="button" class="btn btn-outline-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="dropdownMenuButton2"> Type 1 </button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton3"> <a class="dropdown-item" href="#">Type 2</a> <a class="dropdown-item" href="#">Type 3</a> </div> </div> 

 $(".dropdown-menu a").click(function () { var text = $(this).text(); $(this).closest('.btn-group').find('.btn').html(text); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="btn-group"> <button type="button" class="btn btn-outline-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="dropdownMenuButton2"> Option 1 </button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton2"> <a class="dropdown-item" href="#">Option 2</a> <a class="dropdown-item" href="#">Option 3</a> </div> </div> <div class="btn-group"> <button type="button" class="btn btn-outline-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="dropdownMenuButton2"> Type 1 </button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton3"> <a class="dropdown-item" href="#">Type 2</a> <a class="dropdown-item" href="#">Type 3</a> </div> </div> 

Try 尝试

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

$(this).parent().find(".btn:first-child").html($(this).text());

});

暂无
暂无

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

相关问题 在单击时,仅将类应用于单击的元素,而不是将所有元素都应用于同一类 - On click only apply class to element clicked, not all elements with the same class 具有相同类的多个元素,仅获取单击元素的文本 - Multiple elements with the same class, get the text of only the clicked element 对于具有相同类的多个元素,仅影响jquery中的单击元素 - affect only the clicked element in jquery for multiple elements with same class 只删除点击的元素,不删除 canvas 上的其他元素 - Deleting only the clicked element, not the other elements on the canvas 如何只选择具有相同类名的元素列表中的第一个元素 - How select only the first element of a list of elements with the same class name 如何获取被单击的元素的类名并使用相同的类名来操纵其他元素? - How to get the class name of the element that was clicked on and use that same class name to manipulate the other elements? 针对所有班级,仅选择单击的元素 - Targeting all of class, select only clicked element 使用jQuery更改单击元素的样式并将该样式从具有相同类的其他元素中删除 - Using jQuery to change style of clicked element and removing that style from other elements with same class 对具有相同类的其他元素进行动画处理,但不对此元素进行动画处理 - Animate other elements with same class, but not this element 如何选择具有相同类的div元素并对单击的元素做些什么 - How to select div element with the same class and do something on clicked element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM