javascript - 仅使用 Jquery 将链接分配给图像并使其可点击
问题描述
我有一个与一些链接链接的图像滑块,在桌面版本中,当您单击图像时,它会正确定向到链接,但是当我切换到移动版本并单击图像时,链接不起作用,我尝试使用此 Jquery 解决此问题:
jQuery (document) .ready (function () {
jQuery ('. fullwidthbanner-container ul> li: nth-child (3) img'). each (function () {
var currentImage = jQuery (this);
currentImage.wrap ("<a target='_self' href='http://jquerybyexample.blogspot.com'" + currentImage.attr("href") + "'</a>");
});
});
但它仍然没有工作,有没有办法让这些图像超链接?
解决方案
建议尝试以下方法。
jQuery(function($) {
$('.fullwidthbanner-container > ul > li:eq(3) > img').each(function(i, el) {
$(el).wrap(function() {
return "<a href='http://jquerybyexample.blogspot.com" + $(this).attr("src") + "'></a>";
});
});
});
查看更多:https ://api.jquery.com/wrap/
图像通常具有src
属性而不是href
.
推荐阅读
- api - 如何通过rest api(或ambari rest api)捕获活动的namenode IP
- android - 如何从文件中动态设置 imageView?
- r - 从 R 中的 ARMA 模型中获取 AR(p) 值
- css - 带有网格布局的卡片部分
- excel - 根据 sheet1 上复选框的值取消隐藏 sheet2 上的行
- java - 启动进程'命令'C:\Program Files\AdoptOpenJDK\jdk-11.0.6.10-hotspot\bin\java.exe''
- reactjs - React 新手:任何帮助将不胜感激
- websocket - 连接来自 Angular 项目的 Laravel 回声
- excel - 我可以从未打开的工作簿中提取特定数据,例如使用 VBA 的 v-lookup 吗?
- python - Django - 迭代时如何减少数据库查询