Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用带有jquery的img src动态填充锚href_Javascript_Jquery - Fatal编程技术网

Javascript 使用带有jquery的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')); $(

基本上,我试图抓取图像的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(函数(){
返回“”;
});
$('#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" />';
});