简体   繁体   English

Webview JavaScript fontSize,color,backgroundColor不起作用

[英]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... 我正在将html webside从url加载到webview。。。我有Seekbar。在更改时,它应该增加或减少。它是fontSize ...但是我无法成功...而且,textColor和背景色也无法使用...此外,可以更改正文文本...

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. 上面的代码在seekbar更改后更改了整个webview。

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 所以,我知道加载Internet url到webview加载javascript是可行的,但是样式效果却不行。我当时以为加载url的子元素会覆盖父元素,所以这样做

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 @tiny阳光说在桌面chrome上测试后,我在想,子元素覆盖了问题

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

work on desktop chrome. 在桌面chrome上工作。 but in android it have different syntax cause of string literal and not worked.. 但在android中,它具有字符串文字的不同语法原因,因此无法正常工作。

   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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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