我正在尝试使用此代码创建一个库。
如果我有两个单独的容器用于按钮和内容,它是否有效但有没有办法只使用一个容器?
此外,不使用额外的类,如(一,二,三......)
HTML:
<div id="content">this is a box</div>
<button>First</button>
<div class="info">hahahaha</div>
<button>Second</button>
<div class="info">hahahaha2</div>
<button>Third</button>
<div class="info">hahahaha3</div>
JS:
$(".info").hide();
$("button").click(function () {
//replace stuff inside #content with .info in the div under each box
$("#content").html($(this).find(".info"));
});
答案 0 :(得分:0)
改变这个:
$("#content").html($(this).find(".info"));
到此:
$("#content").html($(this).next(".info").text());
<强> jsFiddle example 强>
.find()
经历一个元素的孩子;你想要.next()
获得下一个兄弟元素。
答案 1 :(得分:0)
添加以下两行:
$('.info').hide();
$(this).next('div:first').show();
所以它看起来像:
$(".info").hide();
$("button").click(function () {
$('.info').hide();
$(this).next('div:first').show();
//replace stuff inside #content with .info in the div under each box
$("#content").html($(this).find(".info"));
});