首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Jquery更改Html块

使用Jquery更改Html块
EN

Stack Overflow用户
提问于 2012-03-28 19:49:16
回答 3查看 1.1K关注 0票数 0

我需要用一个div的内容替换另一个div的内容。

我有类似这样的<div><span>texta</span></div>,我需要替换为页面中另一个div的内容。

如何用jquery替换div的内容?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-28 19:54:31

假设你有这样的div ...

代码语言:javascript
运行
复制
<div align="center" id="divId">
</div>

要更改内容..

代码语言:javascript
运行
复制
$('.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;
});
票数 2
EN

Stack Overflow用户

发布于 2012-03-28 19:54:59

有一个很酷的网站,叫做jQuery.com,它有一个叫做Documentation的功能,在那里你可以读到很多关于操纵你的“超文本标记语言”的很酷的东西:

http://api.jquery.com/category/manipulation/

编辑:要让它不那么像巨魔,请尝试以下操作:

代码语言:javascript
运行
复制
var stuff = $('<your-element>').clone();
            $('<the-other-element>').html(stuff);
票数 2
EN

Stack Overflow用户

发布于 2012-03-28 19:57:44

代码语言:javascript
运行
复制
<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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9906879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档