Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.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 JS/jQuery:如何自动包装<;a href>;周围的标签<;img/>;基于img src的href是动态的?_Javascript_Jquery_Dynamic_Href_Word Wrap - Fatal编程技术网

Javascript JS/jQuery:如何自动包装<;a href>;周围的标签<;img/>;基于img src的href是动态的?

Javascript JS/jQuery:如何自动包装<;a href>;周围的标签<;img/>;基于img src的href是动态的?,javascript,jquery,dynamic,href,word-wrap,Javascript,Jquery,Dynamic,Href,Word Wrap,注意:我对Javascript非常陌生,到目前为止只编写了基于jQuery的非常基本的脚本。 不过我学得很快 我所追求的是一种: 1) 识别标签 2) 阅读img标签 3) 用 我认为读取每个图像的src并将其写入一个变量,然后读取该变量并将/img_T/替换为/img_L/,然后将其写入一个新变量,该变量可以简单地添加到每个href 我已经走了这么远,但这根本不起作用: /* in the comments 'xxx' represents a different unique image

注意:我对Javascript非常陌生,到目前为止只编写了基于jQuery的非常基本的脚本。 不过我学得很快

我所追求的是一种:

1) 识别标签

2) 阅读img标签

3) 用

我认为读取每个图像的src并将其写入一个变量,然后读取该变量并将/img_T/替换为/img_L/,然后将其写入一个新变量,该变量可以简单地添加到每个href

我已经走了这么远,但这根本不起作用:

/* in the comments 'xxx' represents a different unique image string */
/* This should get the <img src="../img_T/xxx" /> string as text and store it. */
var $imgSrc        =    $(".displaywrapper img").attr("src");

/* This part should use the above sourced <img src="../img_T/xxx" string and replace ../img_T/ of the src with ../img_L/ and store it in var = imgLink. */
var imgLink        =    $imgSrc.text().replace("img_T","img_L");

/* This part should then wrap the <img src="../img_T/xxx" /> so it becomes <a href="..img_L/xxx"><img src="../img_T/xxx" /></a> */
$(".displaywrapper img").each(function(){.wrap("<a href="imgLink"></a>")});
注释中的“
/*xxx”表示不同的唯一图像字符串*/
/*这应该将字符串作为文本获取并存储*/
var$imgSrc=$(“.displaywrapper-img”).attr(“src”);
/*此部件应使用上述来源,以便*/
$(“.displaywrapper img”).each(函数(){.wrap(“”)});
谢谢你的阅读。
Jannis

我认为这应该起到作用:

$(document).ready(function() {
    $(".displayWrapper img").each(function() {
        var src = $(this).attr('src').replace('img_T','img_L');
        var a = $('<a/>').attr('href', src);
        $(this).wrap(a);
    });
});
$(文档).ready(函数(){
$(“.displayWrapper img”).each(函数(){
var src=$(this.attr('src').replace('img_T','img_L');
变量a=$('在做任何事情之前..
第2行:使用jQuery的函数遍历每个图像。
第3行:使用获取当前图像的src,并将
img\u T
替换为
img\u L


第4行:在
标记周围添加一个新的
标记。

如果您只需要单击图像,请执行以下操作:

$(".displayWrapper img").click(function(){
  document.location.href = this.src.replace("img_T", "img_L");
});

我不明白为什么这是必要的,如果他在控制他的HTML,它将在加载文档时运行一次。谢谢!但仍然有一些不工作,至少在我的网站上不工作。如果可以,请看一下它的行动,我保证这不是我的网站公然的插件:我已经添加了上面的代码,但它根本不需要很抱歉。脚本确实有效。错误在其他地方,因为jQuery fancyzoom插件没有获取链接。非常感谢!更新:我发现了其他不在脚本中的错误,现在一切正常。再次感谢。
$(document).ready(function() {
    $(".displayWrapper img").each(function() {
        var src = $(this).attr('src').replace('img_T','img_L');
        var a = $('<a/>').attr('href', src);
        $(this).wrap(a);
    });
});
$(".displayWrapper img").click(function(){
  document.location.href = this.src.replace("img_T", "img_L");
});