通过单击链接切换图像并禁用活动链接
Toggle image by clicking link and disable the active link
我有一个网页,里面有很多div,就像下面的一样。它使用简单的javascript通过点击两个单独的链接来切换图像。见小提琴。
HTML:
<div id="test">
<img src="http://placehold.it/100x100" />
<div class="caption">
<h4>
<a href="javascript:void(0)" onclick="toggleImg0('test')">Image1</a>
<a href="javascript:void(0)" onclick="toggleImg1('test')">Image2</a>
</h4>
</div>
</div>
JavaScript:
function toggleImg1(charName) {
document.getElementById(charName).getElementsByTagName('img')[0].src = "http://placehold.it/150x150";
}
function toggleImg0(charName) {
document.getElementById(charName).getElementsByTagName('img')[0].src = "http://placehold.it/100x100";
}
现在,我想禁用显示相应图像的链接(它应该像普通文本一样)。我该怎么做?
请查看此处的完整网页。
您正在使用JQuery,为什么不使用他的函数呢?给您的链接一个全局类toggle_img
,然后您可以使用数据属性来定义您希望每个链接在单击时更改的src
,最后定义click
事件,并使用.attr()函数更改src
属性,查看下面的示例。
希望这能有所帮助。
$('body').on('click', '.toggle_img', function() {
$('img').attr('src', $(this).data('src'));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">
<img src="http://placehold.it/100x100" />
<div class="caption">
<h4>
<a href="javascript:void(0)" class="toggle_img" data-src='http://placehold.it/100x100'>Image1</a>
<a href="javascript:void(0)" class="toggle_img" data-src='http://placehold.it/150x150'>Image2</a>
</h4>
</div>
</div>
更新
通过添加额外的css类,将活动图像的链接更改为简单文本,例如:
$('body').on('click', '.toggle_img', function() {
$('img').attr('src', $(this).data('src'));
if (!$(this).hasClass("active"))
$(this).addClass('active');
$(this).siblings().removeClass('active');
})
.toggle_link {
color: blue;
text-decoration: underline;
cursor: pointer
}
.active {
color: black;
text-decoration: none;
cursor: default
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">
<img src="http://placehold.it/100x100" />
<div class="caption">
<h4>
<span class="active toggle_link toggle_img" data-src='http://placehold.it/100x100'>Image1</span>
<span class="toggle_link toggle_img" data-src='http://placehold.it/150x150'>Image2</span>
</h4>
</div>
</div>
这里是另一个解决方案
示例
<script>
function toggleImg(charName,obj) {
var image=document.getElementById('test').getElementsByTagName('img')[0];
//store image1 and image2 href
var img_source1= "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT7j4zeqZB4theKrjb13e8XwhXXYpumwYEzTvhYclxrSrzS_5yJ";
var img_source2="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQwoMrE0aNNuYH5AlJ3B-4PT5UMC2fdAwS19xHVsW-rf_BI__tArA";
if(image.src==img_source1){
image.src=img_source2;
//change anchor default styling for anchor press
obj.style='pointer-events: none;color:grey';
//enable anchor default styling for next anchor
obj.nextElementSibling.style='pointer-events:auto'
}
else {
image.src=img_source1
obj.style=' pointer-events: none;color:grey';
obj.previousElementSibling.style='pointer-events:auto'
}
}
</script>
<div id="test">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQwoMrE0aNNuYH5AlJ3B-4PT5UMC2fdAwS19xHVsW-rf_BI__tArA" />
<div class="caption">
<h4>
<a id='anchor1' href="javascript:void(0)" onclick="toggleImg('test',this)">Image1</a>
<a id='anchor2' href="javascript:void(0)" onclick="toggleImg('test',this)">Image2</a>
</h4>
</div>
</div>
这里有一个片段
<script>
function toggleImg(charName,obj) {
var image=document.getElementById('test').getElementsByTagName('img')[0];
var img_source1= "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT7j4zeqZB4theKrjb13e8XwhXXYpumwYEzTvhYclxrSrzS_5yJ";
var img_source2="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQwoMrE0aNNuYH5AlJ3B-4PT5UMC2fdAwS19xHVsW-rf_BI__tArA";
if(image.src==img_source1){
image.src=img_source2;
obj.style='pointer-events: none;color:grey';
obj.nextElementSibling.style='pointer-events:auto'
}
else {
image.src=img_source1
obj.style=' pointer-events: none;color:grey';
obj.previousElementSibling.style='pointer-events:auto'
}
}
</script>
<div id="test">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQwoMrE0aNNuYH5AlJ3B-4PT5UMC2fdAwS19xHVsW-rf_BI__tArA" />
<div class="caption">
<h4>
<a id='anchor1' href="javascript:void(0)" onclick="toggleImg('test',this)">Image1</a>
<a id='anchor2' href="javascript:void(0)" onclick="toggleImg('test',this)">Image2</a>
</h4>
</div>
</div>
JavaScript:
<script type="text/javascript">
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
</script>
HTML
<a href="#" class="bold" onclick="toggle_visibility('caption');">SHOW/HIDE Images</a>
<div id="caption">Put your images in here and stuff</div>
div将在页面加载时隐藏,单击SHOW/HIDE Images文本将切换可见性。
我想这就是你想要的…:)
相关文章:
- 单击我网站中的链接时打开新选项卡,保持当前选项卡处于活动状态
- 如何获取链接以保持活动状态
- UL 手风琴中的活动非活动链接
- 在JavaScript / jQuery中用活动超链接替换粘贴的链接
- 在页面加载时创建/设置活动的默认链接,但在单击其他链接时删除活动链接
- 活动链接未更改颜色
- 如何根据活动锚点自动将类添加到链接中
- 活动页面链接图标颜色
- 带有链接的jquery选项卡会记住最后一个活动项
- 如何在页面刷新时保持选定/活动的HTML链接颜色
- 当子菜单的父级或包含链接处于活动状态时,如何告知子菜单可见
- 如何将活动状态类动态更改(添加)到导航链接
- 滚动到活动链接 jQuery
- 滚动并聚焦到导航栏中的活动链接
- 如何在 jQuery 中为链接我的“活动”滚动菜单创建例外
- 获取左侧导航链接以保持打开和活动状态
- 打开打印窗口处于活动状态的 PDF 链接
- 设置活动链接(不带 ul 列表)
- 将 css 按钮更改为没有链接的 css 活动
- 需要JS保持悬停链接活动