简体   繁体   English

单击button1并将文本添加到div,单击button2并删除文本和CSS样式button1

[英]click on button1 and add text to div, click on button2 and remove text and css style button1

When I click on .add-btn, I change the .add-btn's style to background:#cccccc and change the val() to "-". 当我单击.add-btn时,我将.add-btn的样式更改为background:#cccccc并将val()更改为“-”。

Now when I remove the added text with tr td .list that is a row in a list, I want that .add-btn button that added the deleted row to style back to background:#232323 with val() "+". 现在,当我使用tr td .list删除列表中的一行添加的文本时,我想要那个.add-btn按钮,该按钮将删除的行添加为样式,回到背景:#232323,使用val()“ +”。

The problem I have is that there is 10 buttons with classname .add-btn, and how will I now which of the 10 buttons that has been clicked, to add text? 我的问题是,有10个具有类名.add-btn的按钮,现在如何单击10个按钮中的哪个按钮来添加文本?

 jQuery("tr td .list").live('click', function() { jQuery(this).closest("tr td").remove(); }); jQuery(".add-btn").click(function(event) { jQuery(this).val("-"); jQuery(this).css("background", "#cccccc"); event.stopImmediatePropagation(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <tbody> <tr></tr><tr><td><div class="list"><p class="delete-list-domains">X</p>added text in a list</div></td></tr><tr></tr> </tbody> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn"> 

This is how you can target only a clicked button whilst adding the event to all of them in vanilla JS: 这是在将事件添加到Vanilla JS中的所有事件时,可以仅定位单击按钮的方式:

const btnEls = document.querySelectorAll('.add-btn')

Array.from(btnEls).forEach(btnEl => {
  btnEl.addEventListener('click', () => {
    // do whatever you want to button here (use btnEl var)
  })
})

You have to assign different id for each button, giving same id is wrong. 您必须为每个按钮分配不同的ID,但赋予相同的ID是错误的。

 $("tbody").on('click', '.list', function() { console.log($(this).find("tr")); var btn = $(this).data('id'); $('#'+btn).trigger('click'); $(this).remove(); }); $(".btn-xs").on('click', function(event) { var id = $(this).attr('id'); if($(this).hasClass('add-btn')) { $(this).removeClass('add-btn') .addClass('rm-btn') .val('-') .css('background','#cccccc'); $('tbody').append('<tr><td><div class="list" data-id='+id+'><span class="delete-list-domains">X</span> added text in a list by '+id+'</div></td></tr>'); } else if($(this).hasClass('rm-btn')) { $(this).removeClass('rm-btn') .addClass('add-btn') .val('+') .css('background','#fffff'); $('[data-id="'+id+'"]').remove(); } else { alert('error') } }); 
 .delete-list-domains{ color:red;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table> <tbody id="content"> </tbody> </table> <hr/> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn1"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn2"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn3"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn4"> <input type="button" value="+" class="btn-xs add-btn btn-circle" id="add-btn5"> 

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

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