[英]Replace div content with JQuery
I have a few images that when you click on them, I would like a specific div text to appear underneath. 我有一些图像,当您单击它们时,希望在其下方显示特定的div文本。 This only works on the first click for some reason.
由于某些原因,这仅在首次点击时有效。
scroll-content is the div to place the content. scroll-content是放置内容的div。 massage, zen, soul are the image links.
按摩,禅宗,灵魂是图像链接。 The container divs hold the
<p></p>
information. 容器div保存
<p></p>
信息。
<section id = "image">
<a href="#" ><img id ="massage"></a>...
</section>
<section id = "scroll-content">
<div id = "container1">
...
</div>
</section>
$('#massage').click(function() {
var htmlString = $( '#container2').html();
$('#scroll-content').html(htmlString);
});
$('#zen').click(function() {
var htmlString = $( '#container1').html();
$('#scroll-content').html(htmlString);
});
$('#soul').click(function() {
var htmlString = $( '#container3').html();
$('#scroll-content').html( htmlString );
});
You are replacing html under scroll-content
div which has all container
div
s and hence getting lost after replacing html, thats why it is working for first time. 您正在替换具有所有
container
div
的scroll-content
div下的html,因此在替换html后迷路了,这就是为什么它第一次起作用。
Instead of this, just make show / hide container
div
s which is inside scroll-content
. 取而代之的是,仅使show / hide
container
div
s位于scroll-content
内部。
Like below : 像下面这样:
$('#massage').click(function() {
$('[id^="container"]').hide();
$('#container2').show();
});
$('#zen').click(function() {
$('[id^="container"]').hide();
$('#container1').show();
});
$('#soul').click(function() {
$('[id^="container"]').hide();
$('#container3').show();
});
Actual working code: 实际工作代码:
Your example is not working more than one time because when you fire the instruction 您的示例无法正常运行超过一次,因为您触发指令时
$('#scroll-content').html(htmlString);
You are actually overwriting the content of the section where 您实际上是在覆盖该部分的内容
#container1, #container2, [...]
are stored. 被存储。
<section id = "scroll-content">
<div id = "container1"> //You are going to delete me.
...
</div>
</section>
You must change your code to add one dummy container. 您必须更改代码以添加一个虚拟容器。 as this:
这样:
<section id = "image">
<a href="#" ><img id ="massage"></a>...
</section>
<section id = "scroll-content">
<div id = "dummy_container">
...
</div>
<div id = "container1">
...
</div>
</section>
$('#massage').click(function() {
var htmlString = $( '#container2').html();
$('#dummy_container').html(htmlString);
});
$('#zen').click(function() {
var htmlString = $( '#container1').html();
$('#dummy_container').html(htmlString);
});
$('#soul').click(function() {
var htmlString = $( '#container3').html();
$('#dummy_container').html(htmlString);
});
If you want new content to appear underneath the previous content, you have to append
new content. 如果希望新内容显示在先前内容的下面,则必须
append
新内容。 html()
will replace previous content. html()
将替换以前的内容。
$('#massage').click(function() {
var htmlString = $( '#container2').html();
$('#scroll-content').append(htmlString);
});
$('#zen').click(function() {
var htmlString = $( '#container1').html();
$('#scroll-content').append(htmlString);
});
$('#soul').click(function() {
var htmlString = $( '#container3').html();
$('#scroll-content').append( htmlString );
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.