简体   繁体   English

使用 jQuery 单击时,如何更改一个按钮以切换当前/未来事件?

[英]How do I change one button to toggle current/future events when clicked using jQuery?

I have a page I created showing three pictures of runners.我创建了一个页面,显示了三张跑步者的照片。
Under each picture is a list of three events.每张图片下方是三个事件的列表。 When the user clicks on an event, they will see the info(date/location/city) listed under the event.当用户点击一个事件时,他们将看到事件下列出的信息(日期/地点/城市)。
When they click a button, then everything should change to future events.当他们点击一个按钮时,一切都应该改变为未来的事件。 Along with new info under the new list of future events.随着新的未来事件列表下的新信息。 Here is my HTML:这是我的 HTML:

 $(document).ready(function() { var v = false; $("button#futureEvents").click(function(){ if (v == false){ $("li.sprint").text("Snowball Sprint"); $(".sprint").next().children().replaceWith("<li class='info'>1/14/18<br/>Mosquito Hill<br/>New London</li>"); $("li.iron").text("Winter Warrior"); $(".iron").next().children().replaceWith("<li class='info'>2/06/18<br/>Bay Beach<br/>Green Bay</li>"); $("li.twilight").text("Big Chill Run"); $(".twilight").next().children().replaceWith("<li class='info'>2/24/18<br/>Mid Valley Golf Course<br/>De Pere</li>"); $("li.fox").text("Frosty Indoor Marathon"); $(".fox").next().children().replaceWith("<li class='info'>1/15/18<br/>TryIt Ice Arena<br/>Neenah</li>"); $("li.new").text("Valentine Run"); $(".new").next().children().replaceWith("<li class='info'>2/12/18 <br/>Green Isle Park<br/> De Pere</li>"); $("li.winnebago").text("Snowball Marathon"); $(".winnebago").next().children().replaceWith("<li class='info'>3/03/18 <br/>Menominee Park <br/>Oshkosh</li>"); $("li.cheese").text("Trailbreaker Marathon"); $(".cheese").next().children().replaceWith("<li class='info'>4/02/18 <br/>Leach Amphitheatre<br/> Oshkosh</li>"); $("li.chain").text("Jailbreak Marathon"); $(".chain").next().children().replaceWith("<li class='info'>4/16/18 <br/>Menominee Park <br/>Oshkosh</li>"); $("li.foxCities").text("Cellcom Marathon"); $(".foxCities").next().children().replaceWith("<li class='info'>5/20/18<br/> Lombardi Atrium<br/> Green Bay</li>"); $("button").replaceWith("<button id='currentEvents'>Current Events</button>"); v = true; }//end v==false if statement });//end fututre events button $("button#currentEvents").click(function(){ if (v == true){ $("li.sprint").text("Mini Sprint"); $(".sprint").next().children().replaceWith("<li class='info'>10/30/17<br/>Memorial Park<br/>Appleton</li>"); $("li.iron").text("Iron Horse"); $(".iron").next().children().replaceWith("<li class='info'>11/06/17<br/>Bay Beach Park<br/>Green Bay</li>"); $("li.twilight").text("Twilight Trail"); $(".twilight").next().children().replaceWith("<li class='info'>11/13/17<br/>River's Edge Park<br/>Wrightstown</li>"); $("li.fox").text("Fox River Marathon"); $(".fox").next().children().replaceWith("<li class='info'>10/15/17<br/>Pierce Park<br/>Appleton</li>"); $("li.new").text("NEW Half Marathon"); $(".new").next().children().replaceWith("<li class='info'>10/29/17<br/>Bay Beach Park<br/>Green Bay</li>"); $("li.winnebago").text("Winnebago Run"); $(".winnebago").next().children().replaceWith("<li class='info'>11/27/17<br/>Menominee Park<br/>Oshkosh</li>"); $("li.cheese").text("Cheesehead Marathon"); $(".cheese").next().children().replaceWith("<li class='info'>9/24/17<br/>Pamperin Park<br/>Green Bay</li>"); $("li.chain").text("Chain O'Lakes Marathon"); $(".chain").next().children().replaceWith("<li class='info'>Marathon 10/29/17<br/>Bay Beach Park<br/>Green Bay</li>"); $("li.foxCities").text("Fox Cities Marathon"); $(".foxCities").next().children().replaceWith("<li class='info'>11/12/17<br/>Menominee Park<br/>Oshkosh</li>"); $("button").replaceWith("<button id='futureEvents'>Future Events</button>"); v = false; }//end v==true if statement });//end current events button $("li").click(function() { $(this).next().children().toggle(); });//end click function });//end document ready
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="race_box"> <img src="images/run1.jpg" /><br /> <figcaption>5k/10k Events</figcaption> <div class="races" id="5k"> <h3>5k/10 Events</h3> <ul> <li class="sprint">Mini Sprint</li> <ul> <li class="info">10/30/17<br/>Memorial Park<br/>Appleton</li> </ul> <li class="iron">Iron Horse</li> <ul> <li class="info">11/06/17<br/>Bay Beach Park<br/>Green Bay</li> </ul> <li class="twilight">Twilight Trail</li> <ul> <li class="info">11/13/17<br/>River's Edge Park<br/>Wrightstown</li> </ul> </ul> </div> <!-- End of '5k' div--> </div> <!-- End of 'run1' div--> <nav> <button id="futureEvents">Future Events</button> </nav>

UPDATE: Everything is almost working now.更新:现在一切都差不多了。 However, when I click the future events button, it shows the future events(as it should), the button is changed to current events(as it should) but it will not do anything!但是,当我单击未来事件按钮时,它会显示未来事件(应该如此),按钮更改为当前事件(应该如此)但它不会做任何事情! I can click it to see the future events, but cannot click it again to see the current events.我可以单击它来查看未来的事件,但不能再次单击它来查看当前的事件。 Any thoughts?有什么想法吗? Thank you!谢谢!

If nothing at all is working, probably you have forgotten to include jQuery in your page.如果没有任何效果,可能是您忘记在页面中包含 jQuery。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Adding this line in the html would solve it.在 html 中添加这一行将解决它。

Since i have added it in the snippet when i edited your question and the values are changing, i am writing this answer.由于我在编辑您的问题时已将其添加到代码段中并且值正在更改,因此我正在编写此答案。

You need jQuery file because, $.您需要 jQuery 文件,因为$. is a jQuery function which is similar to typing jquery.是一个类似于输入jquery.的 jQuery 函数jquery.


Another issue i found was that you are replacing the class of the elements.我发现的另一个问题是您正在替换元素的类。 after you click the button, you dont find the sprint class at all!单击按钮后,根本找不到冲刺类!

you could use $(".sprint").html("<li class='snowball'>Snowball Sprint</li>");你可以使用$(".sprint").html("<li class='snowball'>Snowball Sprint</li>"); instead of $(".sprint").replaceWith("<li class='snowball'>Snowball Sprint</li>")而不是$(".sprint").replaceWith("<li class='snowball'>Snowball Sprint</li>")

or if you you want the class to be snowball, then you could not acess the children of sprint class because that does not exist!或者,如果您希望课程成为滚雪球,那么您将无法访问 sprint 课程的孩子,因为那不存在!

Also add a class to the ul Element like:还要向 ul 元素添加一个类,例如:

<ul class="SprintList">
    <li class="info">10/30/17<br/>Memorial Park<br/>Appleton</li>
</ul>

Since ul cannot be a child of an li element , you would make another class for this element which is supposed to be under sprint element.由于ul 不能是 li 元素的子元素,因此您将为该元素创建另一个类,该类应该在 sprint 元素下。

and use the jquery like: $(".sprintList .info").html("1/14/18<br/>Mosquito Hill<br/>New London");并使用 jquery,如: $(".sprintList .info").html("1/14/18<br/>Mosquito Hill<br/>New London");
to change the date and place.更改日期和地点。


 $(document).ready(function() { var v = false; $("button#futureEvents").click(function() { if (v == false) { /*if (.info==show){ $(".info").hide(); };//end info if statement*/ $(".sprint").html("<li class='snowball'>Snowball Sprint</li>"); $(".sprintList .info").html("1/14/18<br/>Mosquito Hill<br/>New London"); $(".iron").html("<li class='winter'>Winter Warrior</li>"); $(".ironList .info").html("2/06/18<br/>Bay Beach<br/>Green Bay"); $(".twilight").html("<li class='bigChill'>Big Chill Run</li>"); $(".twilightList .info").html("2/24/18<br/>Mid Valley Golf Course<br/>De Pere"); $("button").replaceWith("<button id='currentEvents'>Current Events</button>"); v = true; } //end v==false if statement }); //end fututre events button $("button#currentEvents").click(function() { }); //end current events button $("li").click(function() { $(this).next().children().toggle(); }); //end click function }); //end document ready
 <!--I add the JQuery here--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="race_box"> <img src="images/run1.jpg" /><br /> <figcaption>5k/10k Events</figcaption> <div class="races" id="5k"> <h3>5k/10 Events</h3> <ul> <li class="sprint">Mini Sprint</li> <ul class="sprintList"> <li class="info">10/30/17<br/>Memorial Park<br/>Appleton</li> </ul> <li class="iron">Iron Horse</li> <ul class="ironList"> <li class="info">11/06/17<br/>Bay Beach Park<br/>Green Bay</li> </ul> <li class="twilight">Twilight Trail</li> <ul class="twilightList"> <li class="info">11/13/17<br/>River's Edge Park<br/>Wrightstown</li> </ul> </ul> </div> <!-- End of '5k' div--> </div> <!-- End of 'run1' div--> <nav> <button id="futureEvents">Future Events</button> </nav>

Hope this helps希望这可以帮助

Instead of replace, use the .addClass() and .removeClass() methods in jQuery.使用 jQuery 中的 .addClass() 和 .removeClass() 方法代替替换。

For example:例如:

 $('h1').on('click', function() { $(this).addClass('blue'); })
 h1 { color: red; } .blue { color: blue; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1>Click Me</h1>

Should the ul with the info not be part of the first li?带有信息的 ul 不应该是第一个 li 的一部分吗?

For example:例如:

  <li class="sprint">Mini Sprint
  <ul>
    <li class="info">10/30/17<br/>Memorial Park<br/>Appleton</li>
  </ul>
  </li>

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

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