简体   繁体   English

jQuery->如何找到具有特定ID的div,然后添加class('activeFeaturedNews');

[英]JQuery -> How can I find a div with specific id then addClass('activeFeaturedNews');

$('.btnReadMore').click(function(){
   var tempId = $(this).attr('id');
   $('div#'+tempId).addClass('activeFeaturedNews');
});

From the first page, When I click on an anchor tag it will pass its Id to the next page then find the div with the same id then add class. 从第一页开始,当我单击锚标记时,它将其ID传递到下一页,然后找到具有相同ID的div,然后添加类。

On click of the anchor tag you can keep the id in the localStorage and on next page use getItem interface to get the stored element 单击锚标记后,您可以将id保留在localStorage并在下一页上使用getItem接口获取存储的元素

$('.btnReadMore').click(function(){
   localStorage.setTime("tempId",$(this).attr('id');
})

On next page 在下一页

var getItemId = localStorage.getItem("tempId");
$("body").$('div#'+tempId).addClass('activeFeaturedNews');

The problem is without redirecting it to the next page how come you can addClass? 问题在于没有将其重定向到下一页,您如何添加addClass? During the click function, first redirect it to the second page. 在单击功能期间,首先将其重定向到第二页。 Then addClass as you did. 然后像您一样执行addClass。

You can simply pass the id as hash parameter in href eg href="http://example.com/about#your-id" 您可以将id作为哈希参数简单地传递到href中,例如href="http://example.com/about#your-id"

Now, on next page, you can get the hash using javascript ie 现在,在下一页上,您可以使用javascript获取哈希,即

var curHash = window.location.hash;
$(""+curHash).addClass('activeFeaturedNews');

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

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