Change body of a page with js

Asked

Viewed 70 times

-1

What I want is the following, I have a normal body of my html page and I want to click on a button with a certain ID the whole body change to another body using javascript or jquery!

(I know very little about js/jquery, I’m learning little by little)

ØBŘĮĞÅĐŐ

  • 1

    The whole body? This is a lot, it is possible to do but it would not be a very "elegant" thing. I suggest you continue studying Js and when you have a good base start studying the Javascript Framework, such as Angular for example.

  • But you want to change styles or html itself ?

1 answer

1


Guy will depend a little on how you set up your CSS as a whole, but you can add a class to body with a new font-family, and with that everything inside will inherit this new font-family of the class added in body

inserir a descrição da imagem aqui

This example is with jQuery, and when you click on BTN he adds the class .fonte in the body with the font-family new.

$( "#btn" ).click(function() {
  $("body").toggleClass("fonte");
  $(this).toggleClass("ativo");
});
body {
  font-family: 'Times New Roman', Times, serif;
}
body.fonte {
  font-family: arial;
}
#btn.ativo {
  background-color: green;
}
#btn {
  background-color: red;
  cursor: pointer;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

<div id="btn">btn</div>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Recusandae quibusdam nostrum, eveniet ullam voluptas blanditiis quos unde pariatur maiores quasi laudantium modi dolorem officia, corporis distinctio doloribus molestias ipsa autem.</p>

  • Thanks, thanks to you Hugocsl had a full idea!

  • @Luckyblack no problem, young man. If you want something more advanced build your CSS using CSS Variables, and with JS you change the value of the variable, here you can read more about this https://answall.com/questions/298995/o-significa-o-specified-no-root-do-css-do-bootstrap/298998#298998 see here tb https://stackoverflow.com/a/38002850/8953758

Browser other questions tagged

You are not signed in. Login or sign up in order to post.