我有一个ul图像和一个主图像。是否可以通过单击CSS中的某个ul来更改主图像的src?通过点击li项来更改img src CSS
1
A
回答
2
我有图像的UL和主图像。是否可以通过单击CSS中的某个ul来更改主图像的 src?
这是可能实现东西接近你的描述只用CSS什么。
您可以充分利用具有tabindex
属性的元素可以关注的事实。然后,您可以在样式规则中使用:focus
伪类。
但是......不言而喻:一旦你从拥有它的元素中移除焦点,调用:focus
的样式规则将不再有任何效果。
工作实例:
div, div img {
display: inline-block;
width: 50px;
height: 50px;
margin: 0 0 2px;
padding: 0;
cursor: pointer;
}
.display-image {
position: relative;
margin-left: 12px;
vertical-align: top;
cursor: default;
}
.display-image img {
position: absolute;
width: 200px;
height: 200px;
opacity: 0;
cursor: default;
transition: opacity 1s ease-out;
}
div:nth-of-type(1):focus ~ .display-image img:nth-of-type(1),
div:nth-of-type(2):focus ~ .display-image img:nth-of-type(2),
div:nth-of-type(3):focus ~ .display-image img:nth-of-type(3),
div:nth-of-type(4):focus ~ .display-image img:nth-of-type(4),
div:nth-of-type(5):focus ~ .display-image img:nth-of-type(5) {
opacity: 1;
}
<h2>Click on any of the thumbnail images</h2>
<div tabindex="0"><img src="http://placekitten.com/205/205" /></div>
<div tabindex="0"><img src="http://placekitten.com/209/209/" /></div>
<div tabindex="0"><img src="http://placekitten.com/210/210/" /></div>
<div tabindex="0"><img src="http://placekitten.com/211/211/" /></div>
<div tabindex="0"><img src="http://placekitten.com/212/212/" /></div>
<div class="display-image">
<img src="http://placekitten.com/205/205" />
<img src="http://placekitten.com/209/209/" />
<img src="http://placekitten.com/210/210/" />
<img src="http://placekitten.com/211/211/" />
<img src="http://placekitten.com/212/212/" />
</div>
0
在我看来,你将需要使用JavaScript。
如果你正在使用jQuery(未经测试)
$(document).on('click', '.ul-li-element-image', function() {
$('.mainImage').attr('src', $(this).attr('src'));
});
尝试沿着这些路线的东西...
+0
是的,它不会帮助我...谢谢 –
相关问题
- 1. 试图通过开关更改img src
- 2. 通过jquery更改img元素的src
- 3. 如何通过点击jQuery来更改iframe src?
- 4. 如何仅通过css更改Img标签src?
- 5. 如何通过点击另一个图像来改变img src的转换
- 6. 更改img src值
- 7. img点击更改视频
- 8. 通过js更改li类
- 9. PHP:通过正则表达式将[img] src [/ img]更改为<img src="src" alt="src" >
- 10. 更改img的src
- 11. JQuery - 如何通过点击链接更改图像src
- 12. AngularJS:通过点击另一个按钮来更改CSS
- 13. 用通用名称更改img的src
- 14. Jquery img src通过Response.OutputStream更新
- 15. 通过点击链接每次更改背景img
- 16. jQuery通过点击锚点链接来更改id元素
- 17. 使用jquery更改img src
- 18. 用jQuery更改img src
- 19. 使用jQuery更改img src
- 20. JQuery和更改img src
- 21. 通过PHP设置img src?
- 22. HTML/JavaScript/CSS:通过点击图片来改变图片
- 23. 更改img scr基于点击图标
- 24. Img/Audio通过document.getElementByID(“...”)更改。src = x +“。png”;不会工作
- 25. 通过片段/插件更改img的src
- 26. 通过url更改iframe src
- 27. 更改CSS和未来点击
- 28. 通过jquery将data-src更改为src
- 29. Jquery在点击li项目时更改div文本值
- 30. img id更改后的图片点击()
没办法的人。你需要利用JavaScript的力量。 – UncaughtTypeError
@UncaughtTypeError我已经看到它发生在某处我; m不太确定如何......我需要构建一些将运行到浏览器的用户已禁用jajo –
CSS无法处理单击事件处理程序,最接近你会使用像':hover'这样的伪状态 - 但这也有其局限性,因为CSS无法选择任何位于初始选择器之前的元素,而且元素必须以非常特定的方式嵌套。如果用户仍然在浏览JavaScript,并在当今时代禁用JavaScript,那么放弃所有希望,谁进入这里! – UncaughtTypeError