如何使用单击功能创建链接,以显示由 jQuery 组成的子内容?
HTML:
<div id="mainContent">
<div class="photoShow"></div>
<div class="photo_nav"></div>
</div>
<div class="photo_panels">
<div class="photo_panel">
<div class="photo_content"></div>
</div>
<div class="photo_panel">
<div class="photo_content"></div>
</div>
<div class="photo_panel">
<div class="photo_content"></div>
</div>
</div>
JavaScript/jQuery:
创建三个链接来显示具有 DOM 元素和引用的照片的“photo_content”:
$('.photo_panels .photo_panel').each(function(index){
$('.photo_nav').append('<a class = "photo_nav_item"></a>');
});
$('.photo_nav a.photo_nav_item').addClass('current');
// Set up Navigation Links
$('.photo_nav a.photo_nav_item').click(function(){
var navClicked = $(this).index();
var Photo = $('.photo_content').get(navClicked);
$(".photo_nav a.photo_nav_item").removeClass('current');
$(".photoShow").removeClass('current');
$(this).addClass('selected');
$(".photoShow").fadeIn('slow', 'swing');
$(".photoShow").html(Photo);
这只需单击两次即可吗?这有什么问题吗?如果我设置下面的代码,这可以工作,但我会丢失内容的 jQuery 函数:
var newPhot = $(Photo).html()
$(".photoShow").html(newPhoto);
HTML:
<div id="mainContent">
<div class="photoShow"></div>
<div class="photo_nav"></div>
</div>
<div class="photo_panels">
<div class="photo_panel">
<div class="photo_content"></div>
</div>
<div class="photo_panel">
<div class="photo_content"></div>
</div>
<div class="photo_panel">
<div class="photo_content"></div>
</div>
</div>
JavaScript/jQuery:
To create three links to display "photo_content" which has DOM element and a photo referenced:
$('.photo_panels .photo_panel').each(function(index){
$('.photo_nav').append('<a class = "photo_nav_item"></a>');
});
$('.photo_nav a.photo_nav_item').addClass('current');
// Set up Navigation Links
$('.photo_nav a.photo_nav_item').click(function(){
var navClicked = $(this).index();
var Photo = $('.photo_content').get(navClicked);
$(".photo_nav a.photo_nav_item").removeClass('current');
$(".photoShow").removeClass('current');
$(this).addClass('selected');
$(".photoShow").fadeIn('slow', 'swing');
$(".photoShow").html(Photo);
This is good for only two clicks? What is wrong with this? If I set the code below this works but I lose jQuery functions of the content:
var newPhot = $(Photo).html()
$(".photoShow").html(newPhoto);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅对现有的 a.photo_nav_items 有效,因此,如果您创建新元素,请尝试
将事件处理程序附加到使用此选择器的任何元素,无论是现在还是将来。
is only valid for existing a.photo_nav_items, so if you create new elements try
to attach the event handler to any element with this selector, now and in the future.