繁体   English   中英

在div中调用javascript

[英]calling javascript inside a div

这是我的代码:

 $(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> 

我想做的是在每个div中都包含按钮(在div“按钮”中),而不是在下面。 因为我希望它们不显示在最后,所以我想在第一页上更改它的值。 但是当我这样做时,它们不再起作用。

代码段不起作用,但是它起作用了,当您单击“下一个”时,将显示下一个div。

这是我想要的:

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

希望它清楚

HTML中的id属性应始终是唯一的。 与其在所有下一个/上一个按钮上使用相同的id ,不如使用一个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