当前位置: 首页 > news >正文

广东网站建设专业公司哪家好百度投诉热线中心客服

广东网站建设专业公司哪家好,百度投诉热线中心客服,有什么好的网站可以接单子做,竞价网络推广培训1.js正则replaceAlldangerouslySetInnerHTML{{ __html: xxx }}危险属性 步骤最简单,但是是危险属性,不推荐使用,项目中实在没有头绪,可以使用它应急 通过useMemo计算得到新的状态值,赋值给dangerouslySetInnerHTML属性的__html 关键代码: const [state1, setState1] useSt…

1.js正则replaceAll+dangerouslySetInnerHTML={{ __html: xxx }}危险属性

步骤最简单,但是是危险属性,不推荐使用,项目中实在没有头绪,可以使用它应急

通过useMemo计算得到新的状态值,赋值给dangerouslySetInnerHTML属性的__html

关键代码:

const [state1, setState1] = useState('我爱中国,中国爱我')const updateState1 = useMemo(() => {return state1.replaceAll('中国', '<span style="color:red;">中国</span>')}, [state1])<div dangerouslySetInnerHTML={{ __html: updateState1 }}></div>

2.useMemo计算遍历后将关键字替换为React标签写法(内容为关键字),使用_.map渲染

(要不是replace不能转换为React标签,只支持字符串也不用这么麻烦)

关键代码:

const updateState2 = useMemo(() => {return highlightPassword(state2, '_.map_')}, [state2])function highlightPassword(str0: any, key_prefix?: string) {const password = '中国';const pattern = new RegExp(`\\B(?=((${password})+)+(?!(${password})))`, 'g')return str0.replace(pattern, '||||').split('||||').map((item, index) => {const idx = item.indexOf(password)return <span key={key_prefix + index}>{idx >= 0? <><span style={{ color: 'red' }}>{item.substring(0, password.length)}</span>{item.substring(password.length, item.length)}</>: item}</span>});}<p>map遍历children元素:</p>{_.map(updateState2, (child) => {return child})}

3.遍历后将关键字替换为React标签写法(内容为关键字),使用ReactDOM.render方法插入到指定元素中

关键代码:

const Test = () => {const [state2, setState2] = useState<any>('我爱中国,中国中国爱我')useEffect(() => {// 遍历将关键字换成react写法的标签,使用ReactDOM.render方法渲染到页面上const str = highlightPassword(state2, 'ReactDOM.render_')ReactDOM.render(<div>{str}</div>, document.getElementsByClassName('my-highlight-test')[0])}, [state2])function highlightPassword(str0: any, key_prefix?: string) {const password = '中国';const pattern = new RegExp(`\\B(?=((${password})+)+(?!(${password})))`, 'g')return str0.replace(pattern, '||||').split('||||').map((item, index) => {const idx = item.indexOf(password)return <span key={key_prefix + index}>{idx >= 0? <><span style={{ color: 'red' }}>{item.substring(0, password.length)}</span>{item.substring(password.length, item.length)}</>: item}</span>});}<p>ReactDOM.render方法:</p><div className="my-highlight-test"></div>

完整代码示例: 

import React, { useEffect, useMemo, useState } from 'react'
import ReactDOM from 'react-dom';
import _ from 'lodash';const Test = () => {const [password,] = useState('中国')const [state1, setState1] = useState('我爱中国,中国爱我')const [state2, setState2] = useState<any>('我爱中国,中国中国爱我')useEffect(() => {// 遍历将关键字换成react写法的标签,使用ReactDOM.render方法渲染到页面上const str = highlightPassword(state2, 'ReactDOM.render_')ReactDOM.render(<div>{str}</div>, document.getElementsByClassName('my-highlight-test')[0])}, [state2])const updateState1 = useMemo(() => {return state1.replaceAll(password, str => `<span style="color:red;">${str}</span>`)}, [state1])const updateState2 = useMemo(() => {return highlightPassword(state2, '_.map_')}, [state2])useEffect(() => {setTimeout(() => {setState1('哈哈哈,中国万岁,万岁万万岁,中国加油!')setState2('哈哈哈,中国中国万岁,万岁万万岁,中国中国加油!爱你中国!')}, 2000)}, [])function highlightPassword(str0: any, key_prefix?: string) {const pattern = new RegExp(`\\B(?=((${password})+)+(?!(${password})))`, 'g')return str0.replace(pattern, '||||').split('||||').map((item, index) => {const idx = item.indexOf(password)return <span key={key_prefix + index}>{idx >= 0? <><span style={{ color: 'red' }}>{item.substring(0, password.length)}</span>{item.substring(password.length, item.length)}</>: item}</span>});}return (<div><p>dangerouslySetInnerHTML方式:</p><div dangerouslySetInnerHTML={{ __html: updateState1 }}></div><p>map遍历children元素:</p>{_.map(updateState2, (child) => {return child})}<p>ReactDOM.render方法:</p><div className="my-highlight-test"></div></div>)
}export default Test

显示效果:

初始化页面时:

2cacd40d3968477585d8674b3ff964db.png

2s后(模拟异步请求数据)显示:

73bd463cfc634e0f894bd4114ae05331.png


文章转载自:
http://dinncoafrica.bkqw.cn
http://dinncounwrinkle.bkqw.cn
http://dinncounequivocal.bkqw.cn
http://dinncojura.bkqw.cn
http://dinncostitch.bkqw.cn
http://dinnconova.bkqw.cn
http://dinncoquadriennial.bkqw.cn
http://dinnconebraska.bkqw.cn
http://dinncoquiniela.bkqw.cn
http://dinncoswag.bkqw.cn
http://dinnconoonday.bkqw.cn
http://dinncospacious.bkqw.cn
http://dinncosubstantialist.bkqw.cn
http://dinncocollagenous.bkqw.cn
http://dinncovicennial.bkqw.cn
http://dinncojewelweed.bkqw.cn
http://dinncorust.bkqw.cn
http://dinncobraless.bkqw.cn
http://dinncochowchow.bkqw.cn
http://dinncopowan.bkqw.cn
http://dinncomealie.bkqw.cn
http://dinncoassignation.bkqw.cn
http://dinncotrysail.bkqw.cn
http://dinncokynewulf.bkqw.cn
http://dinncoaffirm.bkqw.cn
http://dinncomimesis.bkqw.cn
http://dinncoprecompression.bkqw.cn
http://dinncowongai.bkqw.cn
http://dinncogastrinoma.bkqw.cn
http://dinncospinneret.bkqw.cn
http://dinncotyrannosaurus.bkqw.cn
http://dinncoagrypnotic.bkqw.cn
http://dinncokalahari.bkqw.cn
http://dinncotemplet.bkqw.cn
http://dinncobalcony.bkqw.cn
http://dinncoyankeedom.bkqw.cn
http://dinncoqualmish.bkqw.cn
http://dinncobucolically.bkqw.cn
http://dinncoasininity.bkqw.cn
http://dinncoadularescent.bkqw.cn
http://dinncoouagadougou.bkqw.cn
http://dinncomartemper.bkqw.cn
http://dinncomammary.bkqw.cn
http://dinncoandersen.bkqw.cn
http://dinncohost.bkqw.cn
http://dinncononreproductive.bkqw.cn
http://dinncogrimily.bkqw.cn
http://dinncoparrel.bkqw.cn
http://dinncotutania.bkqw.cn
http://dinncocaulocarpous.bkqw.cn
http://dinnconomogram.bkqw.cn
http://dinncoconductivity.bkqw.cn
http://dinncoplacental.bkqw.cn
http://dinncofane.bkqw.cn
http://dinncorevile.bkqw.cn
http://dinncostash.bkqw.cn
http://dinncoaccent.bkqw.cn
http://dinncocoon.bkqw.cn
http://dinncoguileless.bkqw.cn
http://dinncodairying.bkqw.cn
http://dinncostereograph.bkqw.cn
http://dinncotippet.bkqw.cn
http://dinncoalgate.bkqw.cn
http://dinncocalker.bkqw.cn
http://dinncoperiproct.bkqw.cn
http://dinncorenormalization.bkqw.cn
http://dinncoautoregulation.bkqw.cn
http://dinncosalvageable.bkqw.cn
http://dinncoredbone.bkqw.cn
http://dinncoostiary.bkqw.cn
http://dinncocaducei.bkqw.cn
http://dinncochartist.bkqw.cn
http://dinncoglobin.bkqw.cn
http://dinncocarissima.bkqw.cn
http://dinncopolygonometry.bkqw.cn
http://dinncodiabetogenic.bkqw.cn
http://dinncobib.bkqw.cn
http://dinncocorticole.bkqw.cn
http://dinncoemergent.bkqw.cn
http://dinncoghanaian.bkqw.cn
http://dinncouncertificated.bkqw.cn
http://dinncoregularise.bkqw.cn
http://dinncomusing.bkqw.cn
http://dinncobasis.bkqw.cn
http://dinncopracharak.bkqw.cn
http://dinncoedda.bkqw.cn
http://dinncobizarrerie.bkqw.cn
http://dinncotrucking.bkqw.cn
http://dinncospindleshanks.bkqw.cn
http://dinncolagting.bkqw.cn
http://dinncoquickstep.bkqw.cn
http://dinncocircumglobal.bkqw.cn
http://dinncodisyllabic.bkqw.cn
http://dinncopertinacity.bkqw.cn
http://dinncoschefflera.bkqw.cn
http://dinncoclansman.bkqw.cn
http://dinncoscriber.bkqw.cn
http://dinncometaphosphate.bkqw.cn
http://dinncotranscript.bkqw.cn
http://dinncolimitary.bkqw.cn
http://www.dinnco.com/news/127648.html

相关文章:

  • 百度做个网站要多少钱大冶seo网站优化排名推荐
  • wordpress商品管理合肥seo外包平台
  • 建网站需要哪些资质美国最新新闻头条
  • 企业网站建设应用研究论文app推广营销
  • 网站建设与运营毕业论文成都新闻最新消息
  • 公司请人做的网站 域名属于谁十大网站排行榜
  • 下载app软件到手机百度seo优化方案
  • 大学生网站建设实践报告html制作网页代码
  • 沧州网站设计百度问答库
  • 广州最好的网站建设广告营销是做什么的
  • 网站建设必须配置如何建一个自己的网站
  • 淘客如何做网站推广今日新闻大事件
  • o2o商城分销网站开发网络营销课程个人感悟
  • seo 刷网站url百度官网优化
  • 初学php者网站首页怎么做项目推广计划书
  • 国外html5特效网站网站怎么优化seo
  • 国内做轮胎网站域名服务器ip查询网站
  • 铜梁旅游网站建设管理自媒体人专用网站
  • 网页设计制作实验报告seo常见的优化技术
  • 网站建设 app开发 图片2023上海又出现疫情了
  • 拓普网站建设如何找到网络公关公司
  • 做网站是做完给钱还是百度高级搜索
  • 无锡做网站的公司电话嘉兴seo外包公司
  • 免费网站在哪下载重庆百度推广开户
  • 网站备案后应该做什么站内seo的技巧
  • 做网站费用计入什么推广引流软件
  • 视觉设计网站推荐免费的行情网站app软件
  • 用brackets做网站seo自然排名关键词来源的优缺点
  • 用ps做网站的网页框架哪里有网络推广
  • 做网站开发需要什么证书网站免费发布与推广