为了账号安全,请及时绑定邮箱和手机立即绑定

如何在js中通过其值隐藏按钮?

如何在js中通过其值隐藏按钮?

白衣非少年 2022-01-07 21:06:51
如何按其值隐藏按钮?例子 :<button class="mat-raised-button mat-primary" color="primary" mat-raised-button="">    <span class="mat-button-wrapper">RESET</span>    <div class="mat-button-ripple mat-ripple" matripple=""></div>    <div class="mat-button-focus-overlay"></div></button>其实我想隐藏这个有RESET的按钮。但我无法在其类的帮助下隐藏它,因为同一个类正在其他按钮中使用。如果尝试使用类名隐藏所有具有相同类的按钮将被隐藏。有什么帮助吗?
查看完整描述

3 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

你的问题不清楚。你是这个意思吗?


var elements = document.getElementsByClassName("mat-button-wrapper");

for(var i = 0; i < elements.length; i++) {

  elements[i].style.display = "none";

  console.log("Hidden: " + elements[i]);

}


查看完整回答
反对 回复 2022-01-07
?
慕斯王

TA贡献1864条经验 获得超2个赞

如果您想隐藏此按钮:


<button class="mat-raised-button mat-primary" color="primary" mat-raised-button="">

    <span class="mat-button-wrapper">RESET</span>

    <div class="mat-button-ripple mat-ripple" matripple=""></div>

    <div class="mat-button-focus-overlay"></div>

</button>

因为它包含这个词 RESET


您可以使用以下内容:


// Get all relevant buttons

let myButtons = [...document.getElementsByClassName('mat-raised-button')];


// Cycle through buttons

myButtons.forEach((button) => {


  let matButtonWrapper = button.getElementsByClassName('mat-button-wrapper')[0];


  // Execute actions according to the mat-button-wrapper text

  switch(matButtonWrapper.textContent) {


    case ('RESET') : button.style.opacity = '0'; break;

    // case ('Another word here') : actions here; break;

    // case ('Another word here') : actions here; break;

    // case ('Another word here') : actions here; break;

    // case ('Another word here') : actions here; break;

  }


});


查看完整回答
反对 回复 2022-01-07
?
慕村225694

TA贡献1880条经验 获得超4个赞

首先你的问题不清楚,我不能很好地理解你到底想做什么以及你将如何做。如果您使用 JS ,请使用关键字 'this' , 'this' 将作用于按钮触发事件的当前范围。


例子:


document.querySelector('button class').on('click', function() { 


   this.el.style.display = 'none'


});

当然,在事件函数中,您可以获取单击按钮的值并检查它的值。


或者您可以将 clicked el 传递给函数:


document.querySelector('button class').on('click', function(el) { 


           var value = el.value;


           if(value === 'something') {

               el.style.display = 'none'

           }

        });


查看完整回答
反对 回复 2022-01-07
  • 3 回答
  • 0 关注
  • 411 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号