此处的示例:http : //codepen.io/agentmi6/pen/JoZZWm
当我单击[+]图标时,如何在绿色div中显示绿色div的文本?香港专业教育学院尝试了许多不同的方案,但没有奏效,有人可以给我一些建议,我该怎么做,我将非常感谢。
<div id="wrapper">
<div class="container">
<div class="first" id="f">
<div class="set">+</div>
this is the 1st element
</div>
<div class="first" id="s">
<div class="set">+</div>
this is the 2nd element
</div>
<div class="first" id="t">
<div class="set">+</div>
this is the 3rd element
</div>
</div>
<div id="cc"></div>
</div>
的CSS
.first{
margin-top:5px;
border:1px solid black;
width:190px;
height:40px;
background-color:green;
}
#cc{
margin-top:5px;
width:190px;
height:40px;
border:1px solid black;
background-color:grey;
}
.set{
cursor:pointer;
color:#fff;
font-size:33px;
float:right;
border:1px solid white;
}
您必须获取.first div的文本,我建议您将文本放入标签所以它会很容易得到它,所以你没有得到+中的不用彷徨按钮。这是新的JavaScript
$(document).ready(function(){
$('.set').click(function(){
// Get the text inside the span in the parent .first of the clicked .set button
var text = $(this).parent('.first').find('span').text();
$('#cc').text(text);
});
});
HTML看起来像这样
<div class="container">
<div class="first" id="f">
<div class="set">+</div>
<span>this is the 1st element</span>
</div>
<div class="first" id="s">
<div class="set">+</div>
<span>this is the 2nd element</span>
</div>
<div class="first" id="t">
<div class="set">+</div>
<span>this is the 3rd element</span>
</div>
</div>
<div id="cc"></div>
这是更新的CodePen
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句