简体   繁体   中英

Webview JavaScript fontSize,color,backgroundColor is not work

I'm loading html webside from url to webview... And I have Seekbar... On change it should increase or decrease It's fontSize...But I could not succeed... Moreover,textColor and background color also are not worked... In Addition to this,changing body text is worked...

What I done?

Code that are not worked

loadUrl("javascript:(document.body.style.backgroundColor ='red');"); 
loadUrl("javascript:(document.body.style.fontSize='20pt');");
loadUrl("javascript:(document.body.style.color ='yellow');");

Code that is worked

loadUrl("javascript:(document.body.innerHTML ='test');");

Above code changed whole webview after seekbar changed.

So,I understand loaded internet url to webview to load javascript is work, but style effects is not.I was thinking loaded url child elements override parent so did this

loadUrl("javascript:(var elements = document.getElementsByTagName(\"*\");" +
                        "for (var i=0; i < elements.length; i++) \n" +
                        "{    \n" +
                        "    elements[i].style.fontSize ='30rem';\n" +
                        "}" +
                        ");");  

that is not worked aswell

What could be problem??

Update

After @tiny sunlight says test on desktop chrome , I was thinking, child elements override problem

var elements = document.getElementsByTagName("*")
                        for (var i=0; i < elements.length; i++) 
                        {    
                            elements[i].style.fontSize ='20pt'
                        }

work on desktop chrome. but in android it have different syntax cause of string literal and not worked..

   webview.loadUrl("javascript:(var elements = document.getElementsByTagName(\"*\")\n" +
                        "                        for (var i=0; i < elements.length; i++) \n" +
                        "                        {    \n" +
                        "                            elements[i].style.fontSize ='20pt'\n" +
                        "                        });");

I achieve it by a trick.

It's easy to oom, You can try to improve it.

            String b = "javascript:alert(document.getElementsByTagName(\"*\")["+0+"].style.backgroundColor=\"yellow\");";
            StringBuilder sb = new StringBuilder(b);
            for(int i = 0;i<100;i++){
                sb.append("alert(document.getElementsByTagName(\"*\")["+i+"].style.backgroundColor=\"yellow\");");
            }
            a.loadUrl(sb.toString());

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