简体   繁体   English

更改img src单击侦听器以更改第二个img src

[英]change img src click listener to change second img src

I'm currently building a filter with 5 options. 我目前正在建立具有5个选项的过滤器。 The filter has an img with a 'prev' and 'next' div on either side. 过滤器的img两侧各有一个'prev'和' div When you click either div , the img src changes to the adjacent src stored in an array . click任一divimg src更改为存储在array的相邻src This works fine. 这很好。 Now I have a second filter. 现在,我有了第二个过滤器。 This filter will also have 5 options, except the 5 options available will depend on the option selected on the first filter. 该过滤器还将具有5个选项,但5个可用选项将取决于在第一个过滤器上选择的选项。 So filter1>option1 will result in options 1,2,3,4 and 5 being available on the second filter. 因此filter1> option1将导致选项1,2、3、4和5在第二个过滤器上可用。 filter1>option2 will result in options 6,7,8,9 and 10 being available on the second filter. filter1> option2将在第二个过滤器上提供选项6,7、8、9和10。

I'm trying to use the .onload listener so that when the filter one img loads, if filter1 source == A then filter two img src is pulled from array 1. If filter1 source == B then filter two img src is pulled from array 2. 我正在尝试使用.onload侦听器,以便在加载一个img过滤器时,如果filter1 source == A,则从数组1中提取两个img src。如果filter1 source == B,则从中提取两个img src阵列2。

I thought I'd explain my problem in pseudo for clarity. 为了清晰起见,我以为我会用伪解释我的问题。 Here is a jsfiddle of my progress so far. 这是到目前为止我的进步。 http://jsfiddle.net/XujYW/ http://jsfiddle.net/XujYW/

Thanks in advance! 提前致谢!

The most important thing that is wrong in your code, is that you missinterpreted what $(function() { ..}) does. 代码中最重要的错误是,您错解了$(function() { ..})作用。 This is just a shortcut for $(document).ready() . 这只是$(document).ready()的快捷方式。 It's fired after loading the page and NOT when anything changes on the page. 加载页面后触发,而不是页面上发生任何更改时触发。

Then you use a very complicated way to find out the state of your list. 然后,您可以使用一种非常复杂的方法来查找列表的状态。 You should have a look at jquerys data functions which allow you to bind data directly to an element. 您应该看一下jquerys数据函数,该函数允许您将数据直接绑定到元素。 That's way more useable than your reading of img src and parsing it to get a simple index number. 这比您阅读img src并将其解析为一个简单的索引号更有用。 Look here (I left this approach as it is). 看这里 (我照原样保留了这种方法)。

Next, you are repeating your code over and over. 接下来,您将一遍又一遍地重复代码。 This makes it hard to handle especialy if you want to change anything. 如果要更改任何内容,这将使处理起来特别困难。 Better split reapeating parts of code into functions. 更好地将可重复使用的代码部分拆分为功能。 This is what @thinklinux was requesting. 这就是@thinklinux所要求的。

Anyhow, here is a working plunker (I prefer plunker because it's easier to debug and has a built in beautifier which makes code easier to read for those who want to help you). 无论如何,这是一个工作正常的插件(我更喜欢插件,因为它更容易调试,并且具有内置的美化工具,对于想要帮助您的人来说,代码更易于阅读)。

I set up your values to arrays like that: 我将您的值设置为这样的数组:

  // A global array for convenience 
  subs = new Array();
  subs['Shelter'] = ['shelter-one', 'shelter-two', 'shelter-three', 'shelter-four', 'shelter-five'];
  subs['Food'] = ['food-one', 'food-two', 'food-three', 'food-four', 'food-five'];
  subs['Energy'] = ['energy-one', 'energy-two', 'energy-three', 'energy-four', 'energy-five'];
  subs['Transport'] = ['transport-one', 'transport-two', 'transport-three', 'transport-four', 'transport-five'];
  subs['Economy'] = ['economy-one', 'economy-two', 'economy-three', 'economy-four', 'economy-five'];
  setup_effects();

  var filterOne = ['shelter', 'food', 'energy', 'transport', 'economy'];

An handle everyting over a single function: 单个函数上的句柄:

  //Now setup the master filter
  //Parameters are:
  // Element to change
  // Array to use
  // Master filter
  // update on apply
  applyfilter('filter-one', filterOne, true, false);

This function itself calls apllyfilter with the last two parameters changed. 此函数本身将调用apllyfilter,并更改最后两个参数。 This is not a very elegant approach (think spaghetticode!), but at least it works. 这不是一种非常优雅的方法(请考虑使用spaghetticode!),但至少可以奏效。 Find the full code here 在这里找到完整的代码

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

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