简体   繁体   English

使用javascript / jquery,更新设置图像的src的最佳方法是什么?

[英]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("/");
  });
}

fiddle 小提琴

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

相关问题 使用 JavaScript/jQuery 预加载图像的最佳方式? - The definitive best way to preload images using JavaScript/jQuery? 使用 jquery 下载多个图像的最佳方法是什么 - What is the best way to download mulitple images using jquery 使用JavaScript获取和设置单个cookie值的“最佳”方法是什么? - What is the “best” way to get and set a single cookie value using JavaScript 在JavaScript中预加载多个图像的最佳方法是什么? - What is the best way to preload multiple images in JavaScript? 使用JavaScript或Jquery在浏览器中存储本地值的最佳方法是什么? - what is the best way to store local values in browser using Javascript or Jquery? <a>使用 JavaScript/jQuery 获取标签的绝对链接的最佳方法是什么?</a> - What is the best way to get the absolute link of an <a> tag using JavaScript/jQuery? 使用javascript或jquery用GET参数组成链接的最佳方法是什么 - What is the best way to compose a link with GET parameters using javascript or jquery 使用 javascript/jQuery 实现多路切换的最佳方法是什么? - What is the best way to implement multiway toggle using javascript/jQuery? 在 JavaScript 中简化对象的最佳方法是什么(最好使用 Jquery) - What is the best way to simplify Objects in JavaScript (perferably using Jquery) 在 JQuery 中设置 Src 属性的正确方法是什么? - What's The Correct Way To Set Src Attribute In JQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM