简体   繁体   English

在div中调用javascript

[英]calling javascript inside a div

here is my code: 这是我的代码:

 $(document).ready(function(){ $('#next').click(function() { $('.current').removeClass('current').hide() .next().show().addClass('current'); if ($('.current').hasClass('last')) { $('#next').attr('disabled', true); } $('#prev').attr('disabled', null); }); $('#prev').click(function() { $('.current').removeClass('current').hide() .prev().show().addClass('current'); if ($('.current').hasClass('first')) { $('#prev').attr('disabled', true); } $('#next').attr('disabled', null); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <!doctype html> <html lang="en"> <head> <title>javascript problem</title> </head> <body> <div id='div1' class="first current"> <p> here the content of div 1 </p> </div> <div id='div2'> <p> here the content of div 2 </p> </div> //then another 9 divs <div id='div12' class="last"> <input type="submit" value="submit" onclick="submit()"> </div> <div class="buttons"> <button id="prev" disabled="disabled">Prev</button> <button id="next">Next</button> </div> </body> </html> 

what i want to do is have the buttons (in the div 'buttons') in every div and not below. 我想做的是在每个div中都包含按钮(在div“按钮”中),而不是在下面。 because i want them not to show up at the end, and i want to change the value of it on the first page. 因为我希望它们不显示在最后,所以我想在第一页上更改它的值。 but when i do this, they dont work anymore. 但是当我这样做时,它们不再起作用。

the codesnippet does not work but what it does is, when you click 'next' the next div will be shown. 代码段不起作用,但是它起作用了,当您单击“下一个”时,将显示下一个div。

here is how i want it: 这是我想要的:

<div id='div1' class='first current'>
<p>here the content of div 1</p>
<button id="next">Start</button>
</div>

hope its clear 希望它清楚

The id attribute in HTML should always be unique. HTML中的id属性应始终是唯一的。 Instead of using the same id on all your next/prev buttons, you should use a class instead. 与其在所有下一个/上一个按钮上使用相同的id ,不如使用一个class You can then bind the click event using the class instead and it will apply to all the elements with that class . 然后,您可以改为使用class绑定click事件,它将应用于class所有元素。

<button class="next">Start</button>

$('.next').click(function() { ... });

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

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