[英]Using javascript/jquery, what is the best way to update the src of a set images?
I have a bunch of images on a page that look like this: 我在页面上有一堆像这样的图像:
<img src="/Content/Images/Icons/Theme1/title.png" class="slideExampleImage">
<img src="/Content/Images/Icons/Theme1/test.png" class="slideExampleImage">
<img src="/Content/Images/Icons/Theme1/bill.png" class="slideExampleImage">
As you can see, all of the images have class="slideExampleImage" and all of the files are in a directory with the name of the current theme. 如您所见,所有图像都具有class =“ slideExampleImage”,所有文件都位于具有当前主题名称的目录中。
I have a folder for different themes and I want to allow my users to change the theme so I have radio buttons with each theme name so you can assume i can get the selected theme name into a local variable 我有一个用于不同主题的文件夹,并且我希望允许我的用户更改主题,因此每个主题名称都带有单选按钮,因此可以假定我可以将所选主题名称转换为局部变量
var theme = GetThemeNameFromRadioButton();
am trying to figure out the best way to allow the user to select a different theme (lets say "Theme2") and that will result in the images looking like this 正在尝试找出允许用户选择其他主题的最佳方式(让我们说“ Theme2”),这将导致图像看起来像这样
I have different themes and I want a way to change the theme 我有不同的主题,我想要一种改变主题的方法
<img src="/Content/Images/Icons/Theme2/title.png" class="slideExampleImage">
<img src="/Content/Images/Icons/Theme2/test.png" class="slideExampleImage">
<img src="/Content/Images/Icons/Theme2/bill.png" class="slideExampleImage">
so somehow go in and update the src and replace the one subfolder under the images (Theme1 in this case) with the new theme picked (Theme2 in this case) 所以以某种方式进入并更新src,并用选择的新主题(在本例中为Theme2)替换图像(在本例中为Theme1)下的一个子文件夹。
What is a way to achieve that using jquery / javascript? 使用jquery / javascript实现此目的的方法是什么? NOTE that I don't have any control over the theme name as users can same a theme as any name
注意,我对主题名称没有任何控制权,因为用户可以将主题与任何名称相同
function changeTheme(newTheme) {
// change the "src" attribute
$(".slideExampleImage").attr("src", function(_, oldSrc) {
// split the path
var parts = oldSrc.split(/\//);
// replace the "theme" part of the path with the new one
parts.splice(parts.length - 2, 1, newTheme);
// return/set the new path
return parts.join("/");
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.