[英]How do I hide a particular HTML element that contains a specific word from a webpage?
[英]How to hide an element if the page contains a specific url / word?
使用 JavaScript 當頁面 URL 是 my-website.com/login/?lost_pass=1 時,我試圖隱藏一個包含文本消息的元素。 我設法隱藏了標簽,但它沒有按我的預期工作。
不需要的標簽實際上是隱藏的,但是當頁面是 my-website.com/login 時也會發生同樣的情況,而只有當頁面是 my-website.com/login/?lost_pass=1 時才應該隱藏它
因此,例如,您在 my-website.com/login 上,但是當您單擊“丟失密碼”時,頁面重新加載以顯示不同的表單,因此 URL 變為 my-website.com/login/?lost_pass = 1
誰能幫我這個? 我不明白我錯在哪里。
我試過了,但是標簽在所有頁面上都隱藏了
window.onload = function() {
if (window.location.href.indexOf('?lost_pass=1')) {
//Hide the element.
document.querySelectorAll('#msg_social')[0].style.display = 'none';
}
};
然后我嘗試了,但標簽沒有隱藏在任何頁面中
if (window.location.href.indexOf("?lost_pass=1") != -1) {
$("#msg_social").hide();
}
您的 onload 代碼有一個 if 總是通過:
window.onload = function() {
// this if will always pass, any number will pass that if like -1, 25 etc...
if (window.location.href.indexOf('?lost_pass=1')) {
//Hide the element.
document.querySelectorAll('#msg_social')[0].style.display = 'none';
}
};
最好使用。包含:
window.onload = function() {
if (!window.location.href.includes('?lost_pass=1')) {
//Hide the element.
document.querySelectorAll('#msg_social')[0].style.display = 'none';
}
};
它更短,避免忘記!= -1
的事情
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.