我想使用Javascript将div添加到div内的按钮。
<div class="demo">
<button>Inquire Now</button>
</div>
我想使用Javascript参考演示类,将类 popupnew 添加到按钮。
答案 0 :(得分:4)
简单地
document.querySelector( ".demo button" ).classList.add( "popupnew" );
解释
<强>演示强>
document.querySelector(".demo button").classList.add("popupnew");
&#13;
.popupnew {
color: red;
}
&#13;
<div class="demo">
<button>Inquire Now</button>
</div>
&#13;
答案 1 :(得分:1)
//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;