【问题标题】:How to set class by clicking button in html tables如何通过单击html表格中的按钮来设置类
【发布时间】:2020-06-08 21:41:40
【问题描述】:

我有 html 表格。我想通过单击将其更改为 class

当我改变班级时,我想通过点击他们后面的button来选择每个班级

我的尝试如下。我怎样才能通过点击button获取style

谢谢

    $('.click_btn').on('click', function(e) {
      e.preventDefault();
      style = $(this);   ??
    })

    $('.click_td').on('click', function(e) {
      e.preventDefault();
      $(this).AddClass(style);  ??
    });
    .style1{
        background: rgb(255, 0, 255);
        border-radius: 5px;
    }

    .style2 {
        background: rgb(0, 255, 255);
        border-radius: 5px;

    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table>
      <tr>
        <td class="click_td">color</td>
        <td class="click_td">color 2</td>
      </tr>
    </table>
    <button class="click_btn" class="style1">style1</button>
    <button class="click_btn" class="style2">style2</button>

【问题讨论】:

    标签: javascript jquery html css html-table


    【解决方案1】:

    你不应该在元素中多次使用相同的属性,如果你这样做,第一个属性之后的属性将被忽略。虽然最好的方法是在这里使用 data-* 属性。另外AddClass有错别字,应该是addClass

    您可以尝试以下方法:

    var style;
    $('.click_btn').on('click', function(e) {
      e.preventDefault();
      style = $(this).data('style'); 
    })
    
    $('.click_td').on('click', function(e) {
      $(this).removeClass('style1, style2');
      e.preventDefault();
      $(this).addClass(style);  
      style = '';
    });
    .style1{
      background: rgb(255, 0, 255);
      border-radius: 5px;
      color: red;
    }
    
    .style2 {
      background: rgb(0, 255, 255);
      border-radius: 5px;
      color: green;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table>
      <tr>
        <td class="click_td">color</td>
        <td class="click_td">color 2</td>
      </tr>
    </table>
    <button class="click_btn" data-style="style1">style1</button>
    <button class="click_btn" data-style="style2">style2</button>

    【讨论】:

    • 谢谢,我可以点击按钮选择style吗?我运行代码 sn-p,但我无法重现它。当我点击style1 然后点击td,td 会变成style1..
    • @Heisenberg,我已经更新了答案,请检查:)
    【解决方案2】:
    $('.click_btn').on('click', function(e) {
        e.preventDefault();
        style = $(this).attr('class');  //will return style1/style2
    })
    

    【讨论】:

      【解决方案3】:

      多次使用class 属性不是一个好习惯,我们可以将其替换为data 属性。所以在每次点击按钮时,都会获取data 属性值,然后将该值添加到表 td click_td..

      现在点击按钮就完成了,但这也可以用点击td代替;)

       var $ = jQuery;
       $('.click_btn').on('click', function(e) {
            e.preventDefault();
            style = $(this).data().style; 
           $('.click_td').removeClass('style1 style2').addClass(style)
          })
          .style1{
              background: rgb(255, 0, 255);
              border-radius: 5px;
          }
      
          .style2 {
              background: rgb(0, 255, 255);
              border-radius: 5px;
      
          }
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <table>
            <tr>
              <td class="click_td">color</td>
              <td class="click_td">color 2</td>
            </tr>
          </table>
          <button class="click_btn" data-style="style1">style1</button>
          <button class="click_btn" data-style="style2">style2</button>

      这是第二种方法,这里的样式被复制到一个变量中,然后在点击td时应用到td

      var $ = jQuery;
      var style ='';
      $('.click_btn').on('click', function(e) {
        e.preventDefault();
        style = $(this).data().style;
      })
      
       $('.click_td').on('click', function(){
        $(this).removeClass('style1 style2').addClass(style)
       })
      .style1 {
        background: rgb(255, 0, 255);
        border-radius: 5px;
      }
      
      .style2 {
        background: rgb(0, 255, 255);
        border-radius: 5px;
      }
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <table>
        <tr>
          <td class="click_td">color</td>
          <td class="click_td">color 2</td>
        </tr>
      </table>
      <button class="click_btn" data-style="style1">style1</button>
      <button class="click_btn" data-style="style2">style2</button>

      【讨论】:

        【解决方案4】:

        $(()=>{
        var style = $(this).attr('class');   
        $('.click_btn').on('click', function(e) {
              e.preventDefault();
              style = $(this).attr('class');   
            })
        
            $('.click_td').on('click', function(e) {
              e.preventDefault();
              console.log(style);
              $(this).attr('class','');   
              $(this).addClass(style);  
            });
            })
         .style1{
                background: rgb(255, 0, 255);
                border-radius: 5px;
            }
        
            .style2 {
                background: rgb(0, 255, 255);
                border-radius: 5px;
        
            }
           <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <table>
              <tr>
                <td class="click_td">color</td>
                <td class="click_td">color 2</td>
              </tr>
            </table>
            <button class="click_btn style1">style1</button>
            <button class="click_btn style2">style2</button>

        【讨论】:

          【解决方案5】:
          1. 设置样式时需要保存之前的样式值,以便下次删除。
          2. 并且按钮元素中有两个class 属性。
          3. 考虑使用innerText 而不是class 值。

          let prevStyle = '';
          let style = '';
          
          $('.click_btn').on('click', function(e) {
            e.preventDefault();
            prevStyle = style;
            style = $(this).text();
          })
          
          $('.click_td').on('click', function(e) {
            e.preventDefault();
            $(this).removeClass(prevStyle);
            $(this).addClass(style);
          });
          .style1 {
            background: rgb(255, 0, 255);
            border-radius: 5px;
          }
          
          .style2 {
            background: rgb(0, 255, 255);
            border-radius: 5px;
          }
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <table>
            <tr>
              <td class="click_td">color</td>
              <td class="click_td">color 2</td>
            </tr>
          </table>
          <button class="click_btn style1">style1</button>
          <button class="click_btn style2">style2</button>

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-08
            • 2011-05-11
            相关资源
            最近更新 更多