HarmonyOS Web动态加载html片段,带有#会显示空白问题

<div><span style=\"color: #666666;\">测试测试测试测试测试测试</span></div> 
  --------加载空白 
<html><body><div style='color: rgb(100,100,255)'>测试测试测试测试测试</div></body></html> 
  --------加载成功
  • 1.
  • 2.
  • 3.
  • 4.

在多种场景下,无法避免#号。

HarmonyOS
2024-09-05 10:13:10
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

若html中的富文本中带有注入#等特殊字符,建议使用带有两个空格的loadData函数,将baseUrl和historyUrl置为空。

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-webview-V5#loaddata

demo如下:

// xxx.ets 
import web_webview from '@ohos.web.webview'; 
import business_error from '@ohos.base'; 
 
@Entry 
@Component 
struct WebComponent { 
  controller: web_webview.WebviewController = new web_webview.WebviewController(); 
 
  build() { 
    Column({space:30}) { 
      Button('loadData with #') 
        .onClick(() => { 
          try { 
 
            // 点击按钮时,通过loadData,加载HTML格式的文本数据 
            this.controller.loadData( 
              "<html><body bgcolor=\"white\"><div><span style= \"color:#0909F7\" >测试测试测试测试测试</span></div></body></html>", 
              "text/html", 
              "UTF-8", 
              " ", 
              " " 
            ); 
          } catch (error) { 
            let e: business_error.BusinessError = error as business_error.BusinessError; 
            console.error(`ErrorCode: ${e.code},  Message: ${e.message}`); 
          } 
        }) 
 
      Button('loadData without #') 
        .onClick(() => { 
          try { 
            // 点击按钮时,通过loadData,加载HTML格式的文本数据 
            this.controller.loadData( 
              "<html>" + 
                "<body bgcolor=\"white\">" + 
                "<div>" + 
                "<span style=\"color: rgb(255,100,100);\">" + 
                "测试测试测试测试测试测试" + 
                "</span>" + 
                "</div>" + 
                "</body>" + 
                "</html>", 
              "text/html", 
              "UTF-8" 
            ); 
          } catch (error) { 
            let e: business_error.BusinessError = error as business_error.BusinessError; 
            console.error(`ErrorCode: ${e.code},  Message: ${e.message}`); 
          } 
        }) 
      // 组件创建时,加载www.huawei.com 
      Web({ src: 'www.huawei.com', controller: this.controller }) 
    } 
    .width("100%").height("100%").justifyContent(FlexAlign.Center) 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
分享
微博
QQ
微信
回复
2024-09-05 18:38:21
相关问题
HarmonyOS Web组件加载片段时候不显示
843浏览 • 1回复 待解决
HarmonyOS Web组件加载URL显示空白
804浏览 • 1回复 待解决
使用Web组件加载网页,显示空白
1436浏览 • 1回复 待解决
HarmonyOS web组件加载页面空白
1150浏览 • 1回复 待解决
HarmonyOS 使用web控件加载网页出现空白
1573浏览 • 1回复 待解决
HarmonyOS Web组件加载html文件异常
1469浏览 • 1回复 待解决
HarmonyOS Web加载HTML格式的文本失败
722浏览 • 1回复 待解决
web组件对html文件的加载
1663浏览 • 1回复 待解决
HarmonyOS html富文本显示问题
2259浏览 • 1回复 待解决
HarmonyOS 加载html,图片显示不出来
1001浏览 • 1回复 待解决
HarmonyOS webview加载本地html问题
1885浏览 • 1回复 待解决
HarmonyOS web组件怎么加载html字符串
692浏览 • 1回复 待解决