首页 > 解决方案 > 如何使用javascript创建动态ID,然后链接到轮播上的帖子人口?

问题描述

我希望帖子轮播上的用户将我发送到链接而不是帖子,并且何时过期自动删除。你应该知道我不是javascript专家,我做了这个非常基本的。

这是页面https://thecrescentbeachclub.com/events/upcoming-events/我不知道是否有人可以帮助我改进此代码,或者创建另一个。

客户添加带有特色图片的帖子,然后我们使用此模块显示轮播(他们喜欢现在的显示方式,您应该知道我也没有做这个网站,所以很难改变客户的想法),当我添加了一个自动过期程序,当事件完成时,帖子被删除,一切都很好。情况是,在我发送给您的链接上,我添加了链接的下一个代码,因为我不希望人们转到帖子,我希望他们转到外部链接。

jQuery(function($){
$('.qode-bct-posts .qode-bct-post').each(function(){
$(this).attr('id',$(this).index()+1);
});

$('.qode-bct-posts .qode-bct-post .qode-bct-post-image img').each(function(){
$(this).unwrap();
});

$('#1').wrap('<a style="width: 100%; display: inline-block; vertical-align: top;" href="https://www.eventbrite.com/e/easter-at-the-beach-buffet-brunch-tickets-58504343065" target="_blank" itemprop="url"></a>');

$('#2').wrap('<a style="width: 100%; display: inline-block; vertical-align: top;" href="#" target="_blank" itemprop="url"></a>');

$('#3').wrap('<a style="width: 100%; display: inline-block; vertical-align: top;" href="#" target="_blank" itemprop="url"></a>');

});

现在的问题是,当一个帖子被自动删除时,链接是错误的,例如,第一个帖子的 ID 为 1,但当过期时消失,现在 ID 为 2 的帖子的链接为 1。我有手动更改链接,这就是我们想要避免的。

有人可以帮助我使 ID 动态化,这样当帖子过期并消失时,其他人仍然会保留正确的链接吗?我希望我自己解释。

标签: javascriptjqueryhtmlcsscarousel

解决方案


如果您必须从 html 中删除特定的 Id 元素。将 #1 替换为您想要删除的任何内容。

$('.qode-bct-posts #1').parent().closest('a').remove();

您需要传递 id 才能删除特定元素。


推荐阅读