2016-05-18 59 views
0

可以說,這個頁面jQuery/JavaScript隱藏基於當前頁面的元素?

http://MyWebSite.com/users 

對IM和有此頁面上的鏈接按鈕,可以說

<span class="user"> 
    <a href='{$record->url()}' id="main" class="Small">go to page</a> 
</span> 

如果我點擊它進入例如

http://MyWebSite.com/users/jake 

所以,現在當我在這個頁面上有同樣的按鈕存在,我想用javascript或jQuery隱藏它:)

更多信息:在鏈接的{$record->url()}是動態的去根據用戶的網頁,所以我必須使用{$record->url()}腳本來匹配當前頁面鏈接 這可能嗎?

+0

我不認爲你應該用Javascript/jQuery隱藏這個。如果用戶當前位於該頁面上,則不應在服務器上呈現此按鈕。 – apscience

回答

1

(我在手機上,所以這是我現在做的最好的)

Maybe something like ....

if (window.location.href.replace(location.hash,'') == "http://kodeweave.sourceforge.net/editor/") { 
    $(".nicole").hide() 
} else { 
    $(".michael").hide() 
} 
0

你可以使用jQuery的equals selector來隱藏有一個任何元素href屬性,使用下面的代碼指向目標頁面:

// This assumes that the URL will be populated via your server-side code in 
// { ... } braces 
$('[href="{$record->url()}"]').hide(); 

同樣,如果你只是想隱藏的所有元素這指出了當前的URL,你可以通過一個比特字符串連接的做同樣的事情:

// This would hide any elements that point to the current URL 
$('[href="' + window.location.href + '"]').hide(); 

如果你能避免它雖然,你可能要考慮這個隱藏使用服務器端代碼(即使用條件來確定何時應該/不應該呈現某些元素)。