这就是我们被要求做的事情,接下来的代码是我在修改第二个任务后所做的。 问题是它只有在我禁用强加在item-body上的hide函数时才有效。但是练习说最初应该隐藏起来。你能不能给我一个关于如何在项目体隐藏的情况下获得同样效果的提示?
<h2>
标记(例如Click Me)。 item-body div也包含一些文本,但div最初是隐藏的。如果用户点击<h2>
标题,则div项目正文应向下滑动,其内容将变为可见。再次点击标题后,div应该向上滑动,其内容将变为不可见。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>event 3</title>
<script src="library/jquery.js"></script>
<script>
/*3.Extend your code from the previous task in such a way that a deep copy of the first div with the class my-item can be appended to the body of the document by clicking on a button. Be sure to modify your event handler from the previous task so that it also works for the new elements.*/
$(document).ready(function() {
$('#copy').click(function() {
$('.my-item:last').clone().appendTo('body');
});
/*$('.item-body').hide();*/
$(document).on('click', 'h2', function() {
if ($(this).parent().next().children().is(':visible')) {
$(this).parent().next().children().slideUp();
} else {
$(this).parent().next().children().slideDown();
}
/*$(this).parent().next().children().clone().appendTo('body').css({"color": "red", "border": "2px solid red"});// i first select the parent of my element then i select the next parent and its children (the paragraph inside item-body)*/
});
});
</script>
</head>
<body>
<div class='my-item'>
<div class='item-header'>
<h2>Jesus is the only Hope</h2>
</div><!--closing tag for for div item-header-->
<div class='item-body'>
<p>je leve les yeux vers les monts que jaime dou peut me venir ici le secours. Le secours me vient de lEternel meme</p>
</div><!--closing tag for the div item-body-->
</div><!-- This is the closing tag of div my_item-->
<button id='copy'>copy</button>
</body>
</html>
答案 0 :(得分:0)
看起来你的可见/遍历测试:
$(this).parent().next().children().is(':visible')
匹配第二个p
但你没有隐藏p
,你隐藏了div.item-body
最好的办法是更改slidingown以匹配div
而不是p
:
$('.item-body').hide();
$(document).on('click', 'h2', function() {
if ($(this).parent().next().is(':visible')) {
$(this).parent().next().slideUp();
} else {
$(this).parent().next().slideDown();
}
});
问题在于: div item-body应该向下滑动
答案 1 :(得分:0)
使用display: none
,但在右侧元素中,请检查此元素是否可见。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE HTML>
<html>
<head>
<title>event 3</title>
<script src="library/jquery.js"></script>
<script>
/*3.Extend your code from the previous task in such a way that a deep copy of the first div with the class my-item can be appended to the body of the document by clicking on a button. Be sure to modify your event handler from the previous task so that it also works for the new elements.*/
$(document).ready(function() {
$('#copy').click(function(){
$('.my-item:last').clone().appendTo('body');
});
/*$('.item-body').hide();*/
$(document).on( 'click','h2', function() {
if($(this).parent().next().is(':visible')){
$(this).parent().next().slideUp();
}
else{
$(this).parent().next().slideDown();
}
/*$(this).parent().next().children().clone().appendTo('body').css({"color": "red", "border": "2px solid red"});// i first select the parent of my element then i select the next parent and its children (the paragraph inside item-body)*/
});
});
</script>
</head>
<body>
<div class = 'my-item'>
<div class = 'item-header'>
<h2>Jesus is the only Hope</h2>
</div><!--closing tag for for div item-header-->
<div class = 'item-body' style='display: none'>
<p>je leve les yeux vers les monts que jaime dou peut me venir ici le secours. Le secours me vient de lEternel meme</p>
</div><!--closing tag for the div item-body-->
</div><!-- This is the closing tag of div my_item-->
<button id ='copy'>copy</button>
</body>
</html>
&#13;