怎么在jquery中在div中加一段html

怎么在jquery中在div中加一段html,第1张

<style>

.box1{

    width: 600px

    height:300px

    border: 6px solid #bb0000

     

}

h1{

    border-bottom: 1px solid green

}

 

</style>

 

<div class="box1">

    点击改变内容

</div>

<script src="http://code.jquery.com/jquery.js"></script>

<script>

    $('.box1').click(function(event) {

        $(this).html('=========================================================这是新添贴==================================')

    })

</script>

可以定义一个全局变量,在选取该元素将此元素赋给此变量,在点button时,将此变量append到你所要添加的地方。

比如:

var

tempDOM

function

clickDOM(){

tempDOM

=

$(this)

}

function

clickButton(){

$("#你所要添加到的div或者其他的id").append(tempDOM)

}

利用jq的append()追加函数即可实现,如:

html:

<div class="main">

    <input type="button" value="添加" class="btn" />

</div>

JQ:

$(document).ready(function(){

    $(".btn").click(function(){

        var html = '<div style="width:200px height:30px border:1px dashed red margin-bottom:20px"></div>'

        $(".main").append(html)

    })

})


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6273818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-19
下一篇 2023-03-19

发表评论

登录后才能评论

评论列表(0条)

保存