2

下面的 jQuery 不起作用,但是当var link它等于file:///C:/Users/USER/Desktop/test%20page/Home.html它时,是否有一个 jquery,其中 IFwindow.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>
4

3 回答 3

1

我会使用lastIndexOf并获取在你的情况下的 url 的最后一部分,并用var/Home.html检查它。link

见我的演示

下面的代码,

$(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();
    }
});
于 2012-02-13T22:46:40.517 回答
0
if( winloc.substr(winloc.length-link.length) == link) {
    $('ul li a').remove();
}
于 2012-02-13T22:39:59.693 回答
0

这是我认为是 Jivings 想要写的一个班轮。我认为它比公认的解决方案更优雅http://jsfiddle.net/mendesjuan/qbcpm/10/

$(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();
    }
});
于 2012-02-14T00:59:00.630 回答