简体   繁体   English

使用javascript替换链接中的元素

[英]Replace elements in a link using javascript

I have a link to add event to google calendar which is populated from a database, but the date is formatted yyyy-mm-dd, and the time hh:mm, and i cannot alter this, but google calendar will not accept.我有一个将事件添加到从数据库填充的谷歌日历的链接,但日期格式为 yyyy-mm-dd,时间为 hh:mm,我无法更改此设置,但谷歌日历不会接受。

Can anyone please help me use javascript and the 'replace' function to remove the'-' and ':' from the html please?任何人都可以帮助我使用javascript和“替换”功能从html中删除“-”和“:”吗?

 <a href="http://www.google.com/calendar/event? action=TEMPLATE &text=Tester12 &dates=2014-01-27T22:4000Z/2014-03-20T22:1500Z &details=Oranges &location=Newquay &trp=false &sprop= &sprop=name:" target="_blank" rel="nofollow">Add to my calendar</a>

many thanks.非常感谢。

Fetch the href link from tag and store it in a variable.从标签中获取 href 链接并将其存储在一个变量中。

var linkStr = "http://www.google.com/calendar/event?action=TEMPLATE&text=Tester12&dates=2014-01-27T22:4000Z/2014-03-20T22:1500Z&details=Oranges&location=Newquay&trp=false&sprop=&sprop=name:";

var re = /&dates=.*?&/g;
var result = re.exec(linkStr);

if(result!=null){

             var replaceStr = result[0].replace(/[-|:]/g,'');
             var finalLink = linkStr.substr(0,result["index"]) + replaceStr + linkStr.substr(result["index"]+replaceStr.length);
             console.log(finalLink);
}else{
       alert('link invalid');
}

This will remove all the '-' and ':' from dates parameter string and will store that link in 'finalLink' var.这将从日期参数字符串中删除所有“-”和“:”,并将该链接存储在“finalLink”变量中。 Hope it helps.希望能帮助到你。

I have been on the sniff for the whole code solution, and witha bit of mix and match, came up with this, AND IT SEEMS TO WORK!!!!!!我一直在嗅探整个代码解决方案,并进行了一些混合和匹配,想出了这个,它似乎有效!!!!!! But please feel free to edit into perfection!但请随时编辑成完美!

 <script> var linkStr = "http://www.google.com/calendar/event?action=TEMPLATE&text=Example Event&dates=2018-12-16T10:3500Z/2018-12-16T12:0000Z&details=Trip to town&location=No mans land&trp=false&sprop=&sprop=name:"; var re = /&dates=.*?&/g; var result = re.exec(linkStr); if(result!=null){ var replaceStr = result[0].replace(/[-|:]/g,''); var finalLink = linkStr.substr(0,result["index"]) + replaceStr + linkStr.substr(result["index"]+replaceStr.length); console.log(finalLink); }else{ alert('link invalid'); } </script> <a href="" class="finalLink">Add Event</a> <script> (function() { Array.prototype.forEach.call(document.querySelectorAll("a.finalLink"), function(link) { link.href = finalLink; }); })(); </script>

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

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