需求:js给html添加css

目前发送请求,请求回html:<div class="articleRecommendation_con">
            <a href="https://weekly.com"><img src="https://img.com" alt=""></a>
            <a href="https://weekly.com">
            <div class="articleRecommendation_con_right">
                <p>财周</p>
                <p>最新封面报道|烟草反腐风暴</p>
                <p><span>文|财周 </span> <span>05月26日 21:58</span></p>
            </div>
            </a>
        </div>    
    
    
    
        <div class="articleRecommendation_con">
            <a href="https://www.com102059517.html"><img src="https://img.com" alt=""></a>
            <a href="https://www.com">
            <div class="articleRecommendation_con_right">
                <p>能源</p>
                <p>中创新航5月底集中解约应届毕业生 </p>
                <p><span>文|财周</span> <span>05月27日 11:33</span></p>
            </div>
            </a>
        </div>    
    
    
    
        <div class="articleRecommendation_con">
            <a href="https://www.com"><img src="https://img.com" alt=""></a>
            <a href="https://www.com">
            <div class="articleRecommendation_con_right">
                <p>能源</p>
                <p>中创新航5月底集中解约应届毕业生 </p>
                <p><span>文|财周</span> <span>05月27日 11:33</span></p>
            </div>
            </a>
        </div>    
    
    
    
        <div class="articleRecommendation_con">
            <a href="https://www.com"><img src="https://img.com" alt=""></a>
            <a href="https://www.com">
            <div class="articleRecommendation_con_right">
                <p>能源</p>
                <p>中创新航5月底集中解约应届毕业生 </p>
                <p><span>文|财周</span> <span>05月27日 11:33</span></p>
            </div>
            </a>
        </div>    
    
    
    
        <div class="articleRecommendation_con">
            <a href="https://www.com"><img src="https://img.com" alt=""></a>
            <a href="https://www.com">
            <div class="articleRecommendation_con_right">
                <p>能源</p>
                <p>中创新航5月底集中解约应届毕业生 </p>
                <p><span>文|财周</span> <span>05月27日 11:33</span></p>
            </div>
            </a>
        </div>    
    
    
    
        <div class="articleRecommendation_con">
            <a href="https://www.com"><img src="https://img.com" alt=""></a>
            <a href="https://www.com">
            <div class="articleRecommendation_con_right">
                <p>能源</p>
                <p>中创新航5月底集中解约应届毕业生 </p>
                <p><span>文|财周</span> <span>05月27日 11:33</span></p>
            </div>
            </a>
        </div>    

调用接口,但是如果将返回数据的css写在文件中

是不会生效的,这时候应该如何?

我们先将写在文件中的css,const一个css参数,将css放进去

const css = `.taTitle {
                display: block;
                border-top: 2px solid rgba(250, 250, 250, 0.36);
                height: 35px;
                line-height: 35px;
                font-family: PingFangSC-Regular, '微软雅黑';
                font-size: 16px;
                text-align: center;
                margin-bottom: 14px;
            }
            .articleRecommendation_con{
                clear: both;
                margin: 6px 0px;
                overflow: hidden;
            }
            .articleRecommendation_con a:nth-child(1) {
                float: left;
                width: 28%;
                height: 66px;
                padding: 5px 0px;
            }
            a:link {
                text-decoration: none;
                color: #ffffff;
            }
            .articleRecommendation_con a:nth-child(1) img {
                width: 92%;
            }
            .articleRecommendation_con img {
                overflow-clip-margin: content-box;
                overflow: clip;
            }
            .articleRecommendation_con a:nth-child(2) {
                display: block;
                width: 72%;
                float: left;
            }
            .articleRecommendation_con_right p:nth-child(1) {
                display: none;
            }
            .articleRecommendation_con p {
                display: block;
                margin-block-start: 1em;
                margin-block-end: 1em;
                margin-inline-start: 0px;
                margin-inline-end: 0px;
            }
            .articleRecommendation_con_right p:nth-child(2) {
                margin: 0px;
                line-height: 22px;
                word-wrap: break-word;
                text-align: left;
                font-size: 14px;
            }

            .articleRecommendation_con_right p:nth-child(3) {
                margin: 0px;
                line-height: 20px;
                word-wrap: break-word;
                text-align: left;
            }
            .articleRecommendation_con_right p:nth-child(3) span:nth-child(1) {
                display: none;
            }
            .articleRecommendation_con_right p:nth-child(3) span:nth-child(2) {
                color: #ececec;
                line-height: 20px;
                font-family: PingFangSC-Regular, '微软雅黑';
                font-size: 12px;
                text-align: left;
            }`

const sheet = new CSSStyleSheet();
    sheet.replaceSync(css);
    document.adoptedStyleSheets = [sheet];

然后就css生效了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值