简体   繁体   English

在点击列表中更改img src

[英]Change img src on click in list

I'm trying to make an image which changes based on clicking on images in a list. 我正在尝试制作一个图像,该图像根据单击列表中的图像而改变。

The only trouble is, the images in the list have their url embedded in a style as a background-image. 唯一的麻烦是,列表中的图像将其URL嵌入为背景图像。

How would I call the url of the img src to the new image? 如何将img src的网址称为新图片?

<ul id="swatchList_att1"><li class="Name AttributeSwatch In_stock colourSwatch" id="attributeSwatch_1" data-attname="att1" data-attvalue="Deep Blue (001)" data-atrsku="0012345" style="background-image: url(https://color.adobe.com/api/v2/themes/2022184.png);">

EDIT 编辑

Wow, great response time. 哇,响应时间很棒。

and

Wow, I phrased this incredibly poorly yesterday. 哇,我昨天的措辞非常差。 Apologies. 道歉。

The list "swatchList_att1" is a group of color swatches which are shrunk to 50% of the jpeg size. 列表“ swatchList_att1”是一组缩小到jpeg大小的50%的色样。

All I'm trying to do is create a sort of "Preview" image which sits in a different div and will show a selected color swatch at an inflated size (110%). 我要做的就是创建一种“预览”图像,它位于不同的div中,并且将以放大的大小(110%)显示选定的色板。 On page load, it would be the first color in the list. 在页面加载时,它将是列表中的第一种颜色。 When a click occurs on a color in the group of color swatches, the "Preview" image would change to what color was clicked. 当在色样组中的某个颜色上单击时,“预览”图像将更改为单击的颜色。

I can't make changes to how these list items work (ie change it to "data-background") because the functionality behind the scenes would break. 我无法更改这些列表项的工作方式(即,将其更改为“数据背景”),因为幕后功能会中断。 Annoyingly, I can't do much about that. 令人讨厌的是,我对此无能为力。 These list objects need to have their image defined in "style", for whatever reason (I don't know). 无论出于何种原因(我不知道),这些列表对象都需要以“样式”定义其图像。 I can, however, add an onclick function to the list (if I had something that could apply it to all items in the list, which all have very long and different names). 但是,我可以将onclick函数添加到列表中(如果我可以将其应用于列表中的所有项目,它们的名称和名称都非常长,则可以)。

Thus I need to extrapolate the url from the "style" onclick and have "Preview" img src change to that url. 因此,我需要从“样式” onclick推断URL,并将“ Preview” img src更改为该URL。

In response to BearSkyView: 回应BearSkyView:

Since it's a seperate image, this isn't exactly what I meant. 由于它是单独的图像,因此这与我的意思不完全相同。 Also, I'd need something which applied an onclick to everything in the list. 另外,我需要将onclick应用于列表中所有内容的内容。

In response to somethinghere: 在此回应:

That is more closely along the lines of what I'd like to accomplish, but as I can't change the way these list objects are made, this sadly won't work for me. 这与我想要完成的工作更加接近,但是由于我无法更改这些列表对象的制作方式,因此,这对我来说是行不通的。

http://api.jquery.com/css/ http://api.jquery.com/css/

$('#attributeSwatch_1').css("background-image", new src here);

EDIT: Are you looking for something like this? 编辑:您是否正在寻找这样的东西? http://jsfiddle.net/rsqtL805/ http://jsfiddle.net/rsqtL805/

Since you are using data-attributes anyway, why don't you put it into one? 既然您还是在使用data-attributes ,为什么不将其归为一类呢? Say data-background="URL" . data-background="URL"

<img src="" id="myImageElement" alt="large image" />
<img src="path/to/image.jpg" data-background="path/to/image.jpg" onclick="setImg(this); return false;" alt="thumbnail" />

function setImg(obj){
    var img = obj.getAttribute('data-background');
    document.getElementById("myImageElement").attribute("src", img);
}

Otherwise you can read the background image property and split it in javascript to retrieve the URL and do with it as you like: 否则,您可以阅读背景图片属性,并在javascript中拆分它以获取URL并根据需要进行处理:

var img = this.style.backgroundImage;
    img = img.split("rl(")[1].split(")")[0];

The above will take the following string: 上面将采用以下字符串:

url(path/to/image.jpg);

And split it first into: 并首先将其拆分为:

[0] => u
[1] => path/to/image.jpg)

And then we split the element at place 1 at the ) and select the remainder at index 0. 然后我们在地点1以分割元件)和在索引0处选择的余数。

[0] => path/to/image.jpg
[1] =>

With that you could do 这样你就可以做

document.getElementById("myImageElement").attribute("src", img);

to set the src of the image element to the just retrieved source. 将图像元素的src设置为刚检索到的源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM