jquery添加html元素

14 篇文章 0 订阅
1 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
    <style type="text/css">
     .blue{
            color:blue
        }
    </style>
</head>
<body>

<!-- jq 添加html和样式-->
<p class="p1" >添加1</p>
<p class="p2">添加2</p>
<p class="p3">添加3</p>
<button id="btn1">添加html</button>
<button id="btn2">添加css样式</button>
<button id="btn3">添加html和css样式</button>

<!--<p style="color: #006699;height: 16px">添加3要添加的内容</p>   直接把这段代码赋值到 $(".p3").append("")中就行,-->
<script type="text/javascript">
    $(document).ready(function(){
      var  a="<p style=\"color: #006699;height: 16px\">添加3要添加的内容</p>\n"

        $("#btn1").click(function(){
            $(".p1").append("<b>Hello</b>");
        });
        $("#btn2").click(function(){
            $(".p2").addClass("blue");
        });
        $("#btn3").click(function(){
            $(".p3").append("<p style=\"color: #006699;height: 16px\">添加3要添加的内容</p>\n");
        });
        $("#btn3").click(function(){
            $(".p3").append(a);
        });


    });
</script>
</body>
</html>

 

添加css和html: 

html:

<div id="up"></div>

<button nz-button nzType="primary" (click)="search()">搜索</button>

ts:

style=' <style type="text/css">\n' +
    '     .blue{\n' +
    '            color:blue\n' +
    '        }\n' +
    '    </style>'
move
search(){
    // css + html
    this.move=this.style+"<label class='blue'>这是你动态添加的内容</label>"
    $("#up").append(this.move);
}

注意:css里的类名要和html的类名一致,如何写出这样格式的数据。

css:在普通<style></style>写好后复制到ts里

html:在html里写好复制到ts里就好

 

 


 

 
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值