vue 通过v-html动态渲染的html元素 设置样式问题 $nextTick

例如,我们从后端接口中获取来这么一大串html代码需要填充至我们页面当中去


image.png

通过v-html设置上去后,我们在style里面设置样式是设置不上去的
这个时候就要用到$nextTick,通过js来设置样式

<p v-html="text" class="text" id="text"></p>
              if(res.data.code == 200) {
                    console.log(res)
                    let dataList = res.data.data.content
                    let text = document.getElementById('text')
                    for(let i = 0; i < dataList.length;i++) {
                        for(let j = 0; j < dataList[i].length;j++) {
                            this.text = dataList[i][0].post_content
                            this.$nextTick(function () {
                                for(let i = 0;i < text.children.length;i++) {
                                    text.children[i].style.marginBottom = '20px'
                                    text.children[i].children[0].style.fontSize = '12px'
                                    text.children[i].children[0].style.lineHeight = '20px'
                                    text.children[i].children[0].style.color = '#666666'
                                    text.children[i].children[0].style.width = '100%'
                                    if(text.children[2].children[0].nodeName == 'IMG') {
                                        text.children[i].style.textIndent = 0
                                    } else {
                                        text.children[i].style.textIndent = '2em'
                                    }
                                }
                            })
                        }
                    }
                }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,773评论 1 45
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,789评论 0 3
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,093评论 1 92
  • 面试题一:https://github.com/jimuyouyou/node-interview-questio...
    R_X阅读 1,751评论 0 5
  •   DOM 1 级主要定义的是 HTML 和 XML 文档的底层结构。   DOM2 和 DOM3 级则在这个结构...
    霜天晓阅读 1,587评论 1 3

友情链接更多精彩内容