【问题标题】:jQuery Change Icons Dynamically On Click FunctionjQuery 在点击功能时动态更改图标
【发布时间】:2018-09-10 07:13:35
【问题描述】:
我有这个带有图标的链接巢。
当一个链接被点击时,与之关联的图标将变为一个加载图像,以显示该链接处于活动状态。
问题是在第二次或更多点击时,加载图像不会变回图标并显示多个加载图像。
如何让它只显示一个加载图像并恢复之前点击的图标。
HTML 和脚本
$('.parent a').click(function(a) {
a.preventDefault();
var $icons = $(this).siblings('i');
$($icons).replaceWith('<img class="active" src="https://cdn.dribbble.com/users/323893/screenshots/2077235/buffer.gif" width="30" height="30">');
$('img.active').not($icons).replaceWith('<i class="typcn typcn-thumbup"></i>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site1">first link</a>
</div>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site2">second link</a>
</div>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site3">third link</a>
</div>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site4">forth link</a>
</div>
【问题讨论】:
标签:
javascript
jquery
html
ajax
replacewith
【解决方案1】:
您的实现的问题是 .not($icons) 不排除当前的 img 元素,因为 .replaceWith() 返回从 DOM 中删除的原始元素。
缓存对img.active 的引用,稍后用它替换元素。
$('.parent a').click(function(a) {
a.preventDefault();
//Cache active images
var images = $('img.active');
//Replace i
$(this).siblings('i').replaceWith('<img class="active" src="https://cdn.dribbble.com/users/323893/screenshots/2077235/buffer.gif" width="30" height="30">');
//Replace images
images.replaceWith('<i class="typcn typcn-thumbup"></i>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site1">first link</a>
</div>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site2">second link</a>
</div>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site3">third link</a>
</div>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site4">forth link</a>
</div>
创建一个 jQuery 对象并将其与 .not() 和 .replaceWith() 函数一起使用。
$('.parent a').click(function(a) {
a.preventDefault();
var image = $('<img class="active" src="https://cdn.dribbble.com/users/323893/screenshots/2077235/buffer.gif" width="30" height="30">');
$(this).siblings('i').replaceWith(image);
$('img.active').not(image).replaceWith('<i class="typcn typcn-thumbup"></i>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site1">first link</a>
</div>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site2">second link</a>
</div>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site3">third link</a>
</div>
<div class="parent">
<i class="typcn typcn-thumbup"></i>
<a href="site4">forth link</a>
</div>
【讨论】:
-
嗨!你能帮我找到这篇文章中持续问题的解决方法吗?问题是here。谢谢你。
【解决方案2】:
请试试这个:-
$('.parent a').click(function(a){
a.preventDefault();
var $icons = $(this).siblings('i');
$('img.active').not($icons).replaceWith('<i class="typcn typcn-thumbup"></i>');
$($icons).replaceWith('<img class="active" src="https://cdn.dribbble.com/users/323893/screenshots/2077235/buffer.gif" width="30" height="30">');
});
【解决方案3】:
试试这个。不要用类属性替换它,在替换'i'标签后添加它。
$($icons).replaceWith('<img src="https://cdn.dribbble.com/users/323893/screenshots/2077235/buffer.gif" width="30" height="30" />');
$($icons).addClass('active');