2
我试图修剪使用JavaScript的域名前后href
的值。例如,http://www.google.com/about-us
应修整为www.google.com
。JavaScript从IndexOf创建子字符串不起作用
var str = "http://www.google.com/about-us";
var str_before = str.replace("http://","");
document.write(str_before); // Returns ("www.google.com/about-us")
// Trim everything after the domain name
var link = str_before.substring(0, str_before.indexOf('/'));
document.write(link); // Returns "www.google.com/about-uswww.google.com"
我不知道为什么会这样。任何帮助将不胜感激!
'VAR一个=使用document.createElement( “A”); a.href = str;一个举办方; //“www.google.com”' – 2016-12-29 17:23:39
不,它是'document.write'欺骗你。使用'console.log'来检查你的代码的结果。 – Teemu
不要使用document.write,忘记它存在。为什么初学者课程教人们使用它,它不再是90/00s。 – epascarello