Javascript 使用带有jquery的img src动态填充锚href
基本上,我试图抓取图像的SRC,将其包装在一个锚中,然后使用img的SRC作为它的HREF,到目前为止,我已经得到了以下代码,但我有点被困在如何完成它的问题上,有什么想法吗Javascript 使用带有jquery的img src动态填充锚href,javascript,jquery,Javascript,Jquery,基本上,我试图抓取图像的SRC,将其包装在一个锚中,然后使用img的SRC作为它的HREF,到目前为止,我已经得到了以下代码,但我有点被困在如何完成它的问题上,有什么想法吗 $('#detail img').wrap(function() { return '<a href="" rel="one" class="fancybox" />'; }); $('#detail img a').attr('src',$('#detail img').attr('src')); $(
$('#detail img').wrap(function() {
return '<a href="" rel="one" class="fancybox" />';
});
$('#detail img a').attr('src',$('#detail img').attr('src'));
$('#detail img').wrap(函数(){
返回“”;
});
$('#detail img a').attr('src'),$('#detail img').attr('src');
$(“#详细信息img”).wrap(函数(){
返回“”;
});
顺便说一下,如果您可以修改初始HTML,那么最好使用JS进行修改。$(“#detail img”).wrap(function(){
$('#detail img').wrap(function() {
return '<a href="' + $(this).attr('src') + '" rel="one" class="fancybox" />';
});
返回“”;
});
您的第二行始终只使用找到的第一个img a
。另外,img a
无论如何都不是有效的选择器,因为a
不能是函数中img
的子元素,此
引用当前元素(特定的”;
});
谢谢,希望我能编辑HTML,这样会更容易:P
$('#detail img').wrap(function() {
return '<a href="' + $(this).attr('src') + '" rel="one" class="fancybox" />';
});
$('#detail img').wrap(function() {
return '<a href="' + this.src + '" rel="one" class="fancybox" />';
});