简体   繁体   English

Mootools Click事件问题

[英]Mootools Click Event Problem

This peace of code works for the first click. 这种和平的代码适用于第一次单击。 but then it seems to not be able to get new ID from next click. 但随后似乎无法从下次点击中获取新的ID。 the idea behind it is to show one piece of a form and with click on a button it hides first and shows second part. 其背后的想法是显示一张表单,然后单击按钮将其首先隐藏,然后显示第二部分。 any idea what im doing wrong? 知道我在做什么错吗? i guess it has something to do with "this" but from my understanding it should get the id from the second link also. 我猜想它与“ this”有关,但据我了解,它也应该从第二个链接中获取ID。

        window.addEvent('domready', function() 
            {
            $('page_2').slide('hide');
            $('page_3').slide('hide');
            $('page_4').slide('hide');
            $('page_5').slide('hide');
            var togglePrefix = 'toggle_', boxPrefix = 'page_', emptyPrefix = '';
            var links = $('submit_box').getElements('a');
            links.addEvent('click', function(e)
                {
                e.stop();
                var id = $(this.get('id').replace(togglePrefix,emptyPrefix));
                var id_new = parseInt($(this).get('id').replace(togglePrefix, emptyPrefix)) + 1; 
                var next = ('page_'+id_new);
                var id_old = $(this.get('id').replace(togglePrefix,boxPrefix));
                $(id_old).set('slide', {duration: 'long', transition: 'linear'});
                $(id_old).slide('out');
                $(next).slide('in');
                });
            });

the html follows this pattern: html遵循以下模式:

<div id="page_1">

    <div id="inhalt-gewinn">
      <div id="gewinn_bild"></div>
      <div id="gewinn_form">
          <form id="gewinnspiel" name="gewinnspiel" method="post" action="<?=$_SERVER[PHP_SELF]; ?>">
              <div id="input_box">
                  <div><input type="radio" name="frage1" value="Kamille" /><span>Kamille</span></div>
                  <div><input type="radio" name="frage1" value="Kaktus" /><span>Kaktus</span></div>
                  <div><input type="radio" name="frage1" value="Krokus" /><span>Krokus</span></div>

              </div>
              <div id="submit_box"><a id="toggle_1" class="frage">nächste Frage...</a></div>

      </div>
      <div id="gewinn_werbung"></div>
    </div>
</div>

If I understand the example, you've got a bunch of divs with id page_1, page_2 and so on. 如果我理解该示例,则您将获得一堆ID为page_1,page_2等的div。 In every div is a div with the id "submit_box". 每个div中都有一个ID为“ submit_box”的div。 When you wrote $('submit_box').getElements('a') it will add the event only to the first div cause an id has to be unique. 当您编写$('submit_box').getElements('a')它只会将事件添加到第一个div中,因为ID必须是唯一的。 You cant have more then one element with a unique id in the page. 您在页面中只能包含一个具有唯一ID的元素。 So to get your example work change the id to a classname and use $$('div.submit_box a'). 因此,要获取示例工作,请将ID更改为类名,然后使用$$('div.submit_box a')。

The use of ID´s multiple times on the page ruined the code! 在页面上多次使用ID会破坏代码! After fixing this it worked fine 修复此问题后,效果很好

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

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