jQuery 图像交换

发布于 2024-12-08 23:01:16 字数 444 浏览 0 评论 0原文

例如,我有 3 个色域。

  • 颜色
  • 完成突出
  • 显示

现在,当用户单击“颜色”时,图像会根据颜色发生变化。当用户单击“完成”时,饰面会发生变化,并且颜色选择保持为他们选择的......等等。

我正在尝试用 jQuery 来完成这个任务。我正在考虑对产品进行命名约定......类似于“Product_Color_Finish_Highlights.jpg”的内容,然后我将结合所做的选择来更改文件名的适当“部分”。然后将新文件名作为“target_image”的“src”发送。

我已经编写了一些内容,例如手风琴菜单和其他简单的内容,但没有任何内容需要更改图像的源。我已经读了几天了,只是无法想出一个好的起点,或者为此制定“攻击计划”。

我不认为我需要太多关于实际 jQuery 的帮助,但只是......从哪里开始? 我该如何攻击这个?

I have 3 color fields... for example.

  • Color
  • Finish
  • Highlights

Now when a user clicks the "Color" the image changes according to the color. When the user clicks "Finish" the finish changes, and the Color selection stays as they have chosen.. and so on.

I am trying to accomplish this in jQuery. I'm thinking of having a naming convention with the products.. something along the lines of "Product_Color_Finish_Highlights.jpg" then I would change the appropriate 'piece' of the filename in conjunction with the selection made. Then send the new filename as the 'src' of the 'target_image'

I have written a few things like accordion menus, and other simple things, but nothing where I would need to change the source of the image. I have been reading now for a couple days, and just unable to wrap my head around a good starting point, or 'plan of attack' for this.

I don't think I need to much help with the actual jQuery, but just rather.. where to begin?
How do I attack this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2024-12-15 23:01:16

从这个开始:

$('#your-image-selector').attr('src', 'product_color_finish_or_whatever.jpg');

或者,澄清一下您的需要,您将获得更具体的帮助。

进入猜测模式。你暗示了一些更基于惯例的东西。假设您有以下标记,可能是由脚本生成的:

<div id="pickers">
  <select id="color">
    <option value="blue">
  </select>
  <img id="color-swatch"/>

  <select id="finish">
    <option value="blue">
  </select>
  <img id="finish-swatch"/>

  <select id="highlights">
    <option value="blue">
  </select>
  <img id="highlights-swatch"/>

您可以使用以下内容驱动 img src 标记:

$('#pickers > select').bind('load change', function(){
  $(this).next().attr('src', 'Product_' + $(this).val() + '.jpg');
});

这不需要任何特定的类或 ID - - 只是 select 位于 img 旁边,并且 img 文件名包含选择器中的值。

那是什么?您不希望您的图像与选择器相邻? OK,然后,将两者通过 picker-id whatever 的 id 约定和 whatever--swatch 的相关 image-id 链接在一起。然后这段代码将起作用:

$('#pickers > select').bind('load change', function(){
  $('#' + $(this).attr('id') + '-swatch')
    .attr('src', 'Product_' + $(this).val() + '.jpg');
});

这应该使用该约定来查找图像并更新其src

这些代码示例都没有经过测试,但它们应该会激发一些可行的东西。

Start with this:

$('#your-image-selector').attr('src', 'product_color_finish_or_whatever.jpg');

Or, clarify a bit what you need and you'll get more specific help.

Entering guess mode. You hinted at something more conventions based. Suppose you had the following markup, possibly generated by a script:

<div id="pickers">
  <select id="color">
    <option value="blue">
  </select>
  <img id="color-swatch"/>

  <select id="finish">
    <option value="blue">
  </select>
  <img id="finish-swatch"/>

  <select id="highlights">
    <option value="blue">
  </select>
  <img id="highlights-swatch"/>

You could drive the img src tags with this:

$('#pickers > select').bind('load change', function(){
  $(this).next().attr('src', 'Product_' + $(this).val() + '.jpg');
});

That doesn't require any specific classes or IDs on anything--just that the selects be next to the imgs and that the img filename include the value from the picker.

What's that? You don't want your images adjacent to the pickers? OK then, link the two together with the id convention of picker-id whatever and related image-id of whatever--swatch. Then this code will work:

$('#pickers > select').bind('load change', function(){
  $('#' + $(this).attr('id') + '-swatch')
    .attr('src', 'Product_' + $(this).val() + '.jpg');
});

This should use that convention to find the image and update its src.

None of these code samples has been tested but they should inspire something that works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文