JS - How to add a style within a new class?

Asked

Viewed 3,214 times

2

It is possible with javascript to add an element to a class?

EX: Have a class:

.post_22 {color:red}

Until then it only has color, but when you click on a link:

<a onclick="minBox()">Seu Poste</a>

could add the CLASS post_22 another style?

I know you can use it with css(), but on the screen there is a setInterval, which means that when it refreshes again it would remove the css() which would be assigned to the class via css(), in which case the direct assignment to the class would not be removed in the next setInterval.

Remembering that the class post_22 There is no actual css file, it can be post_1, post_2, post_3.... This comes according to the ID on the server linked to the post.

If on the server the ID pole: "Hello my friend." for "ID: 66" it generates the class post_ and together with the ID post_66

  • You can put an example on https://jsfiddle.net and share?

  • I don’t see how to create an example of this. What I really need is that when you click on the link <a onclick="minBox()">Your Post</a> a style is added directly to a class.. instead of user css() that falls into the "style attribute".

  • You are using jQuery?

  • yes, @Gerep got the idea?

  • You want to add a new class CSS on your link, that’s it?

3 answers

3

Yes, using the css as you yourself have mentioned:

$('.classe').css('propriedade', 'valor')

Example:

function RefreshClass(){
    $('.classe').css('text-decoration', 'underline');
}

RefreshClass();

function minBox(){
  $('div').append(
    $('<p />').addClass('classe').text('Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi voluptates veritatis inventore voluptatum magnam sed voluptas perferendis porro doloremque quidem natus iste ipsa. Dolore nostrum illo odit dolor incidunt fuga.')
  );

  // Atualiza a Classe novamente.
  RefreshClass();
}
.classe{
    font-family:Arial, Verdana, sans-serif;
    font-size:12px;
    color:#333;
    text-decoration:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button onclick="minBox()">Postar</button>

<div></div>

Obs.: All the code here can be played without jQuery.

  • Yes, but as I mentioned, there is one setInverval. And when he makes a new comeback style="text-decoration: underline;" this assignment will be removed because it did not exist before. @Kaduamaral

  • Just add the function call RefreshClass within the setInterval @Stoned

3


Good afternoon there is no way you change the estylo of a class what you can do is use

$("#ElelementoId").css({"body":"Value","outro":"valor"});

or if you want to write a class in html you can do the following

$("head").append("<style>.minhaclasse{aqui o  meu estilo}</style>");
  • Good idea, Testing...

  • 1

    blz if it works from a qui up and mark as best good luck response

  • 1

    Boa, the of append didn’t even cross my mind. + 1

0

In your css, use something like:

a[class*='post'] {
    color: red;
    font-size: 20px;
}

This rule in your css will apply whatever you want to all elements to with a class that the name contains "post".

Example in Jsfiddle.

Browser other questions tagged

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