How can I do to apply a style only to one or another p?

0

I have a problem that is apparently simple but because of my lack of experience I'm getting screwed. It's the following, I'm doing a database listing of a text, and this text already comes with the <p></p> tags together. This information was registered using Tinymce and it has the option to put background media, but this media comes in a style="background: url('url da imagem')" and this comes within my <p> tag. Dai I need to do a style setup with css but I can not affect only the tag containing the style with the background. I tried to use the jQuery addClass and it worked but only if I clicked the <p> .

--- HTML ----

<p>texto</p>
<p sytle="background: url('exemplo.com')"></p>
<p>outro texto</p>

--- Jquery ---

$(document).ready(function(){
    $('p').click(function(){
        $(this).addClass('teste')
     });
});

---- CSS ---

.teste{
   height: 500px
}
    
asked by anonymous 06.05.2015 / 14:57

2 answers

1

I do not know if I understood the problem well, but it seemed to me that you have trouble catching an element that has style="background: url('url da imagem')" , does not it?

I tried to reproduce your problem in this Fiddle

HTML

<p>texto</p>
<p style="background:yellow">segundo texto</p>
<p>outro texto</p>

JavaScript

var list = document.getElementsByTagName("p");
for(var i = 0; i < list.length; i++)
{
 if(list[i].style.background == 'yellow')
 {
  alert("sim");
     //Função que quiseres fazer caso tenha aquele estilo
 }
 else
 {
    alert("no");
     //Função que quiseres fazer caso não tenha aquele estilo
 }
}

P.S. I put a color instead of a url just for test purposes, but I believe it works in it, you just have to change the condition value of if

I hope I have helped

    
05.06.2015 / 12:46
0

    .azul{
    color:blue
    }
    .vermelho{
    color:red
    }
<p class='vermelho'>teste</p>
<p class='azul'>teste</p>
    
06.05.2015 / 15:01