简体   繁体   English

jQuery无法识别第二页上li项目的点击

[英]jQuery not recognizing click for li item on second page

I'm working to make a couple buttons to change the theme of a website on click. 我正在努力制作几个按钮,以更改点击时网站的主题。 They work on the main page, but when I tried to move them to the second page, they don't work anymore. 它们在主页上工作,但是当我尝试将其移至第二页时,它们不再起作用。 The click isn't even being recognized at all. 甚至根本无法识别点击。

$(document).ready(function(){
  $(document).on("click", "#warm", function() {
    console.log("Clicked!");
    $(b).addClass('color-red');
    $(li).addClass('color-red #pages li');
    $(h1).addClass('color-red h1');
    $('#pages').addClass('color-red #pages');
  });}

Here is the html that goes with it 这是附带的html

<div class="col-md-4 text-center">
  <ul id="colors">
    <li id="warm">
      Warm
    </li>
    <li id="cool">
      Cool
    </li>
  </ul>
</div>

The cool function is exactly the same as the warm, except it uses removeClass instead of addClass. 除了使用removeClass代替addClass之外,cool函数与warm函数完全相同。

$(document).ready(function(){
  $(document).on("click", "#warm", function() {
    console.log("Clicked!");
    $(b).addClass('color-red');
    $(li).addClass('color-red #pages li');
    $(h1).addClass('color-red h1');
    $('#pages').addClass('color-red #pages');
  });}

WOAHHH Back it up partner. WOAHHH备份伙伴。 First you dont ever want to bind to document, UNLESS you are creating future events. 首先,除非您要创建将来的事件,否则您永远不想绑定到文档。 Since you are using the id selector, you should only have 1 of those on the page, which you can rebind if you rebind that particular id to the page. 由于您使用的是id选择器,因此页面上应该只包含其中的1个,如果您将该特定ID重新绑定到页面上,则可以重新绑定。 You dont want to blind to the document because it is quite heavy or something. 您不想因为文档太重或什么而看不到文档。 jquery has to parse the entire DOM everytime there is an html change to check if you added another "#warm" (in your example) to see if it needs to create another event binding to that element. 每次有html更改时,jquery都要解析整个DOM,以检查是否添加了另一个“ #warm”(在您的示例中),以查看是否需要创建另一个绑定到该元素的事件。

So first off your code should look like this 因此,首先,您的代码应如下所示

$(document).ready(function(){
  $("#warm").on("click", function(e) {
    console.log("Clicked!");
    $(b).addClass('color-red');
    $(li).addClass('color-red #pages li');
    $(h1).addClass('color-red h1');
    $('#pages').addClass('color-red #pages');
  });}

now time for some clarification, what do you mean you "move them to the second page"? 现在该澄清一下,您是什么意思“将它们移至第二页”? What code in there says you are moving them ? 那里的什么代码表明您正在移动它们? What is the second page? 什么是第二页?

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

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