[英]on.click remove part of any url in a <div> and then load it
I want to remove /groups/ from any url in my when a user clicks it 当用户单击时,我想从我的任何URL中删除/ groups /
The backstory is that i have a wordpress widget which displays links to pages (BP groups) and i want users to be sent to pages i've made for each group instead when they click (because i wasnt able to make homepages in buddypress). 背景是我有一个wordpress小部件,该小部件显示到页面(BP组)的链接,并且我希望用户单击时将用户发送到我为每个组创建的页面(因为我无法在buddypress中创建主页)。
I've set the page url's to be the same as the group pages but without /groups/ so if it were removed the widget would send users to my pages instead. 我已将页面网址设置为与群组页面相同,但没有/ groups /,因此,如果将其删除,则该小部件会将用户重定向到我的页面。
For example a visitor is sent to: http://focallocal.org/up-cycling-workshop-with-londons-homeless/ instead of http://focallocal.org/groups/up-cycling-workshop-with-londons-homeless/ 例如,访客被发送到: http : //focallocal.org/up-cycling-workshop-with-londons-homeless/而不是http://focallocal.org/groups/up-cycling-workshop-with-londons-无家可归/
I've been researching it for ages and this is the best I've been able come up with (I don't speak js): 我多年来一直在研究它,这是我能想到的最好的方法(我不会讲js):
'onclick="location.replace(/groups/,"")'
edit requested html: 编辑请求的html:
Close Events, Groups and Projects Newest | 关闭活动,团体和项目最新| Active | 活跃 Popular 流行
<ul id="groups-list" class="item-list">
<li class="odd public is-admin is-member">
<div class="item-avatar">
<a href="h t t p://focallocal. org/groups/up-cycling-workshop-with-londons-homeless/" title="Up-cycling Workshop with the Homeless"><img src="h t t p://focallocal. org/wp-content/uploads/group-avatars/4/a4ce0d8153497b96869a9b46e7ff391f-bpthumb.jpg" class="avatar group-4-avatar avatar- photo" width="50" height="50" alt="Group logo of Up-cycling Workshop with the Homeless" title="Up-cycling Workshop with the Homeless"></a>
</div>
<div class="item">
<div class="item-title"><a href="h t t p://focallocal. org/groups/up-cycling-workshop-with-londons-homeless/" title="Up-cycling Workshop with the Homeless">Up-cycling Workshop with the Homeless</a></div>
<div class="item-meta">
<span class="activity">
active 1 week, 1 day ago </span>
</div>
</div>
</li>
<li class="even public is-admin is-member">
<div class="item-avatar">
<a href=" h t t p://focallocal. org/groups/public-relations-marketing-and-advertising-team/" title="Public Relations, Marketing and Advertising Team"><img src="http://focallocal.org/wp-content/uploads/group-avatars/2/05196f60439a3fe01b9855f267805180-bpthumb.jpg" class="avatar group-2-avatar avatar- photo" width="50" height="50" alt="Group logo of Public Relations, Marketing and Advertising Team" title="Public Relations, Marketing and Advertising Team"></a>
</div>
<div class="item">
<div class="item-title"><a href="h t t p://focallocal. org/groups/public-relations-marketing-and-advertising-team/" title="Public Relations, Marketing and Advertising Team">Public Relations, Marketing and Advertising Team</a></div>
<div class="item-meta">
<span class="activity">
active 1 month, 3 weeks ago </span>
</div>
</div>
</li>
<li class="odd
</ul>
<input type="hidden" id="_wpnonce-groups" name="_wpnonce-groups" value="e2870f6aee"><input type="hidden" name="_wp_http_referer" value="/"> <input type="hidden" name="groups_widget_max" id="groups_widget_max" value="300">
</div> </div></div><div id="tab_slide_background" class="tab_slide_corners_right"></div></div></div>
I see you are using jQuery so you can do this: 我看到您正在使用jQuery,因此您可以执行以下操作:
$('#groups-list a').each(function(){
var $this = $(this);
var href = $this.attr('href');
href = href.replace('groups/', '');
$this.attr('href', href);
});
you can also do this: 您也可以这样做:
$('#groups-list a[href*="groups"]').each(function(){ this.href = this.href.replace('/groups', ''); });
plain JavaScript : 纯JavaScript :
var anchors = document.querySelectorAll( "#groups-list a[href*='group']");
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.href = anchor.href.replace('/groups', '');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.