Applying CSS via jQuery to a div called by Javascript?

I have the following:

<div>
<script type="text/javascript" src="someUrl"></script>
</div>

That the script generates a div with some links inside it, so that:

<div>
    <div>
    <a href=""></a>
    </div>
</div>

I need these links to be a specific color with jQuery because this is a way to design a page.

Now I have the following:

<script type="text/javascript">
    $(document).ready(function () {
         $("div a").css("color","#ffffff");
    });
</script> 

The only browser that changes the links to white is Firefox, because I think FF loads pages differently and applies these styles after the script creates the div? How to make this work in all browsers?

+3
source share
2 answers

The code below works, I think this is what you want.

<script type="text/javascript">
    $(document).ready(function() {

    $("#click").click(function(){
     $("#maindiv").append("<div><div><a href='example.com'>AAA</a></div></div>");
     $("#maindiv > div a").css("color","#ffffff");      
       });    
    });
</script>    


<div id="maindiv">
<div>
    <div>
    <a href=""></a>
    </div>
</div>
</div>

<a id="click" href="#">Click Here</a>

After your JS generates a div, you must set the CSS, it will work.

+1

, - , script, style="color: blue;".

, , , , . div, $('#my-div > div a') (, ).

0

Source: https://habr.com/ru/post/1757939/


All Articles