javascript怎么在指定的标签下面添加HTML代码呀

如题所述

<html>

<head>

<style>

p{ background:yellow;}

</style>

<scriptsrc="http://code.jquery.com/jquery-latest.js";></script>

</head>

<body>

<p>I would like to say: </p>

<script>

  $("p").append("<strong>Hello</strong>");

</script>

</body>

</html>


效果:

在p标签内容后面加上了hello

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-11
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="jquery-1.7.js" language="javascript" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(e) {
$("#btn").click(function(){
$(".ShowDIV").html("HELLO!")
});
$("#btn").dblclick(function(){
$(".ShowDIV").html("WORLD!")
});
$("#btn1").click(function(){
$(".ShowDIV").append("WORLD!")
});
$("#btn2").click(function(){
$(".ShowDIV").prepend("China!")
});
$("#btn3").click(function(){
$(".ShowDIV").after("Welcome to China!")
});
$("#btn4").click(function(){
$(".ShowDIV").after("After Member!")
});
$("#btn5").click(function(){
$(".ShowDIV").before("Before Member!")
});
});


</script>
<style>
.ShowDIV{ display:block; width:300px; height:300px; line-height:30px; font-size:20px; text-align:center; background:#009933; border:2px solid #999;}
</style>
</head>
<body>
<div class="ShowDIV">0.0</div>

<br />=============================================<br />
<input id="btn" type="button" value="单击 双击 改变内容" />
<input id="btn1" type="button" value="向后追加 内容" />
<input id="btn2" type="button" value="向前追加 内容" />
<input id="btn3" type="button" value="元素之后插入 内容" />
<input id="btn4" type="button" value="元素之后插入 内容" />
<input id="btn5" type="button" value="元素之前插入 内容" />
</body>
</html>

自己写的一段Jquey代码,可随意添加文本或HTML代码到指定区域。

代码保存下来,修改一下jquery连接,即可使用!

第2个回答  2017-07-07
$("#dd").html("<div>ok</div>");
//向id为dd的标签下添加html代码

第3个回答  2013-04-11

用jquery比较简单

$('#id').append("<p><p>");

第4个回答  2013-04-10
innerHTML?
相似回答