Javascript 如果window.location通过变量以指定值结束
下面的jQuery不起作用,但当Javascript 如果window.location通过变量以指定值结束,javascript,jquery,html,regex,window.location,Javascript,Jquery,Html,Regex,Window.location,下面的jQuery不起作用,但当var链接等于file:///C:/Users/USER/Desktop/test%20page/Home.html确实如此。如果window.location.href以指定变量结尾,如var link=“/Home.html”然后执行javascript $(document).ready(function(){ var winloc = window.location.href; // file:///C:/Users/USER/Desktop/test%2
var链接
等于file:///C:/Users/USER/Desktop/test%20page/Home.html
确实如此。如果window.location.href代码>以指定变量结尾,如var link=“/Home.html”代码>然后执行javascript
$(document).ready(function(){
var winloc = window.location.href; // file:///C:/Users/USER/Desktop/test%20page/Home.html
var link = "/Home.html";
if(winloc==link){
$('ul li a').remove();
}
});
<ul>
<li><a href="Home.html">Home</a></li>
</ul>
$(文档).ready(函数(){
var winloc=window.location.href;//file:///C:/Users/USER/Desktop/test%20page/Home.html
var link=“/Home.html”;
if(winloc==链接){
$('ul li a')。删除();
}
});
我将使用lastIndexOf
获取url的最后一部分,即您的/Home.html
,并使用链接检查它
看我的
代码如下:
$(document).ready(function() {
var winloc = "file:///C:/Users/USER/Desktop/test%20page/Home.html";
var link = "/Home.html";
winloc = winloc.substring (winloc.lastIndexOf("/"));
if (winloc == link) {
$('ul li a').remove();
}
});
这是Jivings写的一行字。我认为这比公认的解决方案更优雅
看起来您想隐藏当前页面的链接?这可能是更好的服务器端处理方式…而且,window.location.pathname
可能会提供您所需的内容(没有所有文件的相对路径://
或http://
垃圾文件),这是正确的方向,但是“伪科迪”太让新手难以理解了,你接受了科林克的答案,让它更容易使用,并因此获得了荣誉,+1
$(document).ready(function() {
var winloc = "file:///C:/Users/USER/Desktop/test%20page/Home.html";
var link = "/Home.html";
winloc = winloc.substring (winloc.lastIndexOf("/"));
if (winloc == link) {
$('ul li a').remove();
}
});
$(document).ready(function() {
var winloc = "file:///C:/Users/USER/Desktop/test%20page/Home.html";
// The regular expression below looks for Home.html at the end of a string
if (/Home\.html$/.test(winloc)) {
$('ul li a').remove();
}
});