简体   繁体   中英

jQuery .css sometimes doesn't work

How can I fix problem with jQuery function ".css" which works only when it is triggred by user (console, button, ...) ?

I'm using interval in .ready function for trigger it but it dosn't work. However .html function is changing the text properly.

$(document).ready(function()
{
    setInterval(function()
    {
        $.get("./getData", function(data)
        {
                $(".text").html("" + data + " %");
                $(".circle").css("border-width", "" + data + "px");
        });
    }, 1000);
});

Other border properties specified properly? Border color and weight? Its working fine when specified.

Your code looks fine, make sure data is numeric value and you have element with class name circle

jsFiddle

Try to call this function on window.onload.

 <script>
 function testFunction()
 {
   setInterval(function()
{
    $.get("./getData", function(data)
    {
            $(".text").html("" + data + " %");
            $(".circle").css("border-width", "" + data + "px");
    });
}, 1000);
 }

 window.onload=testFunction;

 </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM