jQuery 在 DIV 之间复制内容

发布于 2024-10-01 00:56:28 字数 1326 浏览 0 评论 0原文

我正在开发一个完全基于 Javascript 的网站:加载该网站,然后所有页面都由 Javascript 拉入。

所以我这就是我所拥有的:

    <span id="deathWormButton">Death Worm</span>
    <div id="pageContent">
        <p>Thanks for taking the time to view my portfolio!</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
    </div>
    <div id="DeathWormPage" class="page">
        <p>Thanks for taking the time to view my portfolio!</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
    </div>

这是我的 jQuery:

        $(document).ready(function()
        {
            $(".page").hide();
        });
        $("#deathWormButton").click(function()
        {
            $("#pageContent").innerHTML = $("#DeathWormPage").innerHTML;
        });

但它不起作用! (查看此处

那么如何从 div id="DeathWormPage"deathWormButton 时将 code> 插入 div id="pageContent"

I am working on a website that I want to be entirely Javascript based: you load the website, then all the pages are pulled in by Javascript.

So I here's what I have:

    <span id="deathWormButton">Death Worm</span>
    <div id="pageContent">
        <p>Thanks for taking the time to view my portfolio!</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
    </div>
    <div id="DeathWormPage" class="page">
        <p>Thanks for taking the time to view my portfolio!</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
        <p>Placeholder content</p>
    </div>

And here's my jQuery:

        $(document).ready(function()
        {
            $(".page").hide();
        });
        $("#deathWormButton").click(function()
        {
            $("#pageContent").innerHTML = $("#DeathWormPage").innerHTML;
        });

But it doesn't work! (View here)

So how do I copy content from the div id="DeathWormPage" into div id="pageContent" when the deathWormButton is clicked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趁年轻赶紧闹 2024-10-08 00:56:28

jquery 对象没有innerHTML 属性。

$(document).ready(function () {
    $(".page").hide();

    $("#deathWormButton").click(function () {
        $("#pageContent").html($("#DeathWormPage").html())
    })
});

另外,请将其放入文档就绪函数中,否则您将引用不存在的事物。

jquery objects do not have an innerHTML property.

$(document).ready(function () {
    $(".page").hide();

    $("#deathWormButton").click(function () {
        $("#pageContent").html($("#DeathWormPage").html())
    })
});

Also, put it inside the document ready function or you are referencing a non-existant thing.

一指流沙 2024-10-08 00:56:28

您可以执行以下操作:

$("#pageContent").html($("#DeathWormPage").html())

或者

$("#pageContent")[0].innerHTML = $("#DeathWormPage")[0].innerHTML;

后一种方法会获取可用 innerHTML 的实际 DOM 元素。

You can either do:

$("#pageContent").html($("#DeathWormPage").html())

Or

$("#pageContent")[0].innerHTML = $("#DeathWormPage")[0].innerHTML;

The later approach gets you actual DOM element for which innerHTML is available.

甜警司 2024-10-08 00:56:28

这可以将一个 div 的内容传输到另一个 div...祝你好运

body               { font-size: 16px; line-height: 2em;}
.deathWormButton   { border: 2px solid blue; width: 75px; height: 35px; z-index: 200; font-size: 14px; background-color:#00FF33; }
 


$(function(){
$(".deathWormButton").click(
  function () {
    var htmlStr = $("#pageContent").html();
    $("#DeathWormPage").text(htmlStr);
  }
);
});

 



DeathWorm 


And be one deathWormButton, long I stood  And looked down one as far as I could To where it bent in the undergrowth;
 


ok here-- deathworm button please place content here.
 

This works to transfer the content of one div to the other div... good luck

body               { font-size: 16px; line-height: 2em;}
.deathWormButton   { border: 2px solid blue; width: 75px; height: 35px; z-index: 200; font-size: 14px; background-color:#00FF33; }
 


$(function(){
$(".deathWormButton").click(
  function () {
    var htmlStr = $("#pageContent").html();
    $("#DeathWormPage").text(htmlStr);
  }
);
});

 



DeathWorm 


And be one deathWormButton, long I stood  And looked down one as far as I could To where it bent in the undergrowth;
 


ok here-- deathworm button please place content here.
 

喜爱纠缠 2024-10-08 00:56:28

使用

$("#pageContent").replaceWith($("#DeathWormPage"));

应该也可以,并且更加简洁/语义化。

Using

$("#pageContent").replaceWith($("#DeathWormPage"));

should work as well, and is a little more concise/semantic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文