如何使用Javascript将类添加到class =“demo”div中的按钮?

时间:2017-10-31 06:11:12

标签: javascript

我想使用Javascript将div添加到div内的按钮。

 <div class="demo">
     <button>Inquire Now</button>
 </div>

我想使用Javascript参考演示类,将类 popupnew 添加到按钮。

2 个答案:

答案 0 :(得分:4)

简单地

document.querySelector( ".demo button" ).classList.add( "popupnew" );

解释

<强>演示

&#13;
&#13;
document.querySelector(".demo button").classList.add("popupnew");
&#13;
.popupnew {
  color: red;
}
&#13;
<div class="demo">
  <button>Inquire Now</button>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

&#13;
&#13;
//javascript
var d = document.getElementById("btn");
d.className += "popupnew";


//or jquery

$().ready(function() {
  $(".demo button").addClass('popupnew1');
});
&#13;
.popupnew {
  color: red;
}

.popupnew1 {
  color: green;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <button id="btn">Inquire Now</button>
</div>

<div class="demo">
  <button>Inquire Now</button>
</div>
&#13;
&#13;
&#13;