簡體   English   中英

使用js更改頁面上的所有鏈接

[英]Changing all links on page with js

我想刪除頁面上所有鏈接中的“/index.php”

例子:

http://example.com/?hostname=sad2.cherobr.ru&path=/index.php/o-nas

改成:

http://example.com/?hostname=sad2.cherobr.ru&path=/o-nas

用普通的js

var allAnchors = document.querySelectorAll("a");
Array.prototype.slice.call( allAnchors ).forEach( function( el ){
  var href = el.getAttribute( "href" );
  el.setAttribute( "href", href.replace( "/index.php", "" ) );
});

使用替換功能。

 console.log('http://example.com/?hostname=sad2.cherobr.ru&path=/index.php/o-nas'.replace('/index.php', ''));

您可以使用像這樣的replace("url","/index.php")函數來做到這一點。

var ref= document.getElementById("linkId").href;
document.getElementById("linkId").href= ref.replace("/index.php","");

用於將其從頁面上的所有鏈接中刪除。

var refs= document.getElementsByTagName(a);
for(i=0;i<refs.length;i++)
{
  refs[i].href= refs[i].href.replace("/index.php","");

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM