我需要用一个div的内容替换另一个div的内容。
我有类似这样的<div><span>texta</span></div>,我需要替换为页面中另一个div的内容。
如何用jquery替换div的内容?
发布于 2012-03-28 19:54:31
假设你有这样的div ...
<div align="center" id="divId">
</div>要更改内容..
$('.click').click(function() {
// get the contents of the link that was clicked
var linkText = $(this).text();
// replace the contents of the div with the link text
$('#divId').html(linkText);
// cancel the default action of the link by returning false
return false;
});发布于 2012-03-28 19:54:59
有一个很酷的网站,叫做jQuery.com,它有一个叫做Documentation的功能,在那里你可以读到很多关于操纵你的“超文本标记语言”的很酷的东西:
http://api.jquery.com/category/manipulation/
编辑:要让它不那么像巨魔,请尝试以下操作:
var stuff = $('<your-element>').clone();
$('<the-other-element>').html(stuff);发布于 2012-03-28 19:57:44
<div>
<div id="div1">
<span>content 1</span>
</div>
<div id="div2">
<span>content 2</span>
</div>
<div>
<script type="text/javascript">
$(function() {
$("#div1 span").html( $("#div2 span").html() );
});
</script>在这里查看它的工作原理:http://jsfiddle.net/harpax/P7EZw/
https://stackoverflow.com/questions/9906879
复制相似问题