RN 修改webView 引入的HTML文件

webview:用于访问一个网页,类比HTML的a标签.

关于webview的使用,文档写的很清楚,直接照搬就可以了:

import React, {Component} from 'react';
import {WebView} from 'react-native-webview';
import GlobalConfig from '../../../server';

export default  class MyWeb extends Component {
  render() {
    return (
      <WebView
      // URI 这里自己拼接了一下HTML路径
        source={{uri: GlobalConfig.apkHost + '/app/my-view.html'}}
        style={{marginTop: 20}}
      />
    );
  }
}}

现在我需要修改html文件里面的样式,解决办法:
使用webview的injectedJavaScript:在网页加载之前向html文件注入一段js代码(string类型),通过js来修改html文件.

import React, {Component} from 'react';
import {WebView} from 'react-native-webview';
import GlobalConfig from '../../../server';

export default class ProvicyPolicyPage extends Component {
  render() {
    const jsCode = `(
      function() {
        document.querySelector("body").style.color = '#a3a3a3';
        document.querySelector("h3").style.color = '#8ce8f1';
        document.querySelector("body > b:nth-child(2)").style.color = '#8ce8f1';
        document.querySelector("body > b:nth-child(4)").style.color = '#8ce8f1';
     })();`;
    return (
      <WebView
        source={{uri: GlobalConfig.apkHost + '/app/privacy-policy.html'}}
        javaScriptEnabled={true}
        domStorageEnabled={true}
        injectedJavaScript={jsCode}
      />
    );
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值