uniapp uses v-html calling interface, rich text pictures and video adaptive size

If the front-end obtains the background data and does not process it, the following problems will occur: pictures and videos are not fully displayed beyond the view.

Please add image description
deal with

//info 是富文本
<view v-if='info' v-html='replaceWhite(info)'></view>

Call the following method

replaceWhite(html) {
    
     // 处理富文本默认图片,视频大小
			let newContent = html.replace(/<video[^>]*>/gi, function(match, capture) {
    
    
				match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
				match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
				match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
				return match;
			}).replace(/<img[^>]*>/gi, function(match, capture) {
    
    
				match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
				match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
				match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
				return match;
			});
			newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
    
    
				match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
					'max-width:100%;');
				return match;
			});
			newContent = newContent.replace(/<br[^>]*\/>/gi, '');
			newContent = newContent.replace(/\<video/gi,
				'<video style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"').replace(/\<img/gi,
				'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
			return newContent;
		},

Reference boss

If you think the article is good, remember to give it a thumbs up, follow it, and add it to your favorites. Please correct me if there are any mistakes. If you need to reprint it, please indicate the source. Thank you! ! !

Guess you like

Origin blog.csdn.net/m0_49714202/article/details/134825090