[英]Replacing the href for my <a> links using jquery
I have the following jQuery code to add 'target', '_blank'
for all my links, as follow:-我有以下 jQuery 代码为我的所有链接添加
'target', '_blank'
,如下所示:-
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.min.js"></script>
<script>
var pollCount = 0;
$(document).ready(function($){
checkCalendar($);
});
function checkCalendar($) {
//stop after 10 seconds
if (pollCount > 20)
return;
//if the calendar hasn't loaded yet, then wait .5 seconds
if ($('.ms-acal-title a').length < 1) {
pollCount++;
setTimeout(function(){checkCalendar($);}, 500);
}
else {
//the calendar has loaded, so do work
$('.ms-acal-title a' ).attr('target', '_blank');
}
}
</script>
the above code is working perfectly for me.上面的代码对我来说很完美。 but i want to do additional step, where i need to replace the url for all the
<a>
links as follow:-但我想做额外的步骤,我需要替换所有
<a>
链接的网址,如下所示:-
https://*****/Events/DispForm.aspx?ID=4
.https://*****/Events/DispForm.aspx?ID=4
。 where the ID will differ based on the item i am viewing. https://******/_layouts/15/Event.aspx?ListGuid=0579a0325489&ItemId=4
.https://******/_layouts/15/Event.aspx?ListGuid=0579a0325489&ItemId=4
。 so how i can get the Id from the current url and create a new href
for all my related <a>
links?那么我如何从当前 url 获取 Id 并为我所有相关的
<a>
链接创建一个新的href
?
You are almost there, the way you are setting the target="_blank"
, the same way you can update the href
something like你快到了,你设置
target="_blank"
的方式,就像你可以更新href
一样
(this).attr("href", "newurlValue");
For example例如
$("a").each(function() { $(this).attr('target', '_blank'); var id = $(this).attr("href").split("ID=")[1]; $(this).attr("href", `newurlValue/itemId=${id}`); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <a href="https://test.com/Events/DispForm.aspx?ID=4">Test4</a> <a href="https://test.com/Events/DispForm.aspx?ID=5">Test5</a>
Here's a basic example for you to use as guidance:下面是一个基本示例,供您用作指导:
let oldurl = 'https://*****/Events/DispForm.aspx?ID=4';
let params = oldurl.split('=');
let newurl = 'https://******/_layouts/15/Event.aspx?ListGuid=0579a0325489&ID=' + params[1];
$('.ms-acal-title a' ).attr('href', newurl);
Just notice that you may need to adapt the split
part to your use case, for example if you have multiple params you need to split at &
请注意,您可能需要根据您的用例调整
split
部分,例如,如果您有多个参数,则需要在&
拆分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.