jQuery 在 DIV 之间复制内容
我正在开发一个完全基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
jquery 对象没有innerHTML 属性。
另外,请将其放入文档就绪函数中,否则您将引用不存在的事物。
jquery objects do not have an innerHTML property.
Also, put it inside the document ready function or you are referencing a non-existant thing.
您可以执行以下操作:
或者
后一种方法会获取可用
innerHTML
的实际 DOM 元素。You can either do:
Or
The later approach gets you actual DOM element for which
innerHTML
is available.这可以将一个 div 的内容传输到另一个 div...祝你好运
This works to transfer the content of one div to the other div... good luck
使用
应该也可以,并且更加简洁/语义化。
Using
should work as well, and is a little more concise/semantic.