2011-11-10 135 views
7

我看過以前的問題,他們都沒有真的爲我工作,我檢查谷歌和我發現的信息似乎很模糊,所以我想我會在這裏嘗試。隱藏標題標籤上懸停

是否有人知道/或解決懸停時顯示標題標籤的問題。我有一系列的鏈接和圖像,將被分配標題標籤,但是,其中一些將顯示信息,最好不要彈出懸停。

是否有一個全局函數,我可以用它來適用於所有標題標籤的方式?一個例子如下:

<a href="service.php" title="services for cars" /> 

如果可能,我想禁用懸停時出現的標題「汽車服務」。

再次感謝。

回答

9

這應該只是罰款禁用單標題:

<a href="service.php" title="services for cars" onmouseover="this.title='';" /> 

如果您需要的標題後,可以將其還原:

<a href="service.php" title="services for cars" onmouseover="this.setAttribute('org_title', this.title'); this.title='';" onmouseout="this.title = this.getAttribute('org_title');" /> 

這種方式是不通用的,但..有它適用於所有的錨,有這樣的JavaScript代碼:

window.onload = function() { 
    var links = document.getElementsByTagName("a"); 
    for (var i = 0; i < links.length; i++) { 
     var link = links[i]; 
     link.onmouseover = function() { 
      this.setAttribute("org_title", this.title); 
      this.title = ""; 
     }; 
     link.onmouseout = function() { 
      this.title = this.getAttribute("org_title"); 
     }; 
    } 
}; 

Live test case

編輯:申請相同的多個標籤(例如<img>)首先移動代碼的一個函數的核心:

function DisableToolTip(elements) { 
    for (var i = 0; i < elements.length; i++) { 
     var element = elements[i]; 
     element.onmouseover = function() { 
      this.setAttribute("org_title", this.title); 
      this.title = ""; 
     }; 
     element.onmouseout = function() { 
      this.title = this.getAttribute("org_title"); 
     }; 
    } 
} 

然後將代碼更改爲:

window.onload = function() { 
    var links = document.getElementsByTagName("a"); 
    DisableToolTip(links); 
    var images = document.getElementsByTagName("img"); 
    DisableToolTip(images); 
}; 
+0

天才,完美的作品,感謝。 –

+1

乾杯!只是一個音符,窗口。onload'將會「覆蓋」你可能記得的任何其他onload事件。 :) –

+0

編輯也可以,但只適用於標籤,它可以不適用於圖像嗎? –

0

你可以用」 t禁用它們是因爲它是瀏覽器/ html規範的通用部分。但我知道你可以用css和javascript來設計它們,所以display:none應該可以在某處使用。

here

+0

這將工作得很好,但它只有CSS3,我們需要它符合所有瀏覽器。 –

5

如果您使用「標題」標籤的推理是詠歎調合規性,使用ARIA標籤來代替。

<a href="service.php" aria-label="services for cars" /> 
+0

這就是我所需要的。謝謝! –

2

嘗試設置兩個冠軍,一個空的,將被瀏覽器選爲提示,然後你需要的:

<a href="service.php" title="" title="services for cars" /> 
0

雖然這已經在標準JS得到回答。

這是一個jQuery解決方案。

$('a').hover( 
    function() { 
     $(this).attr("org_title", $(this).attr('title')); 
     $(this).attr('title', ''); 
    }, function() { 
     $(this).attr('title', $(this).attr("org_title")); 
    } 
); 
0

一個簡單的方法是將圖像(股利或)的容器上使用CSS:

pointer-events: none; 
cursor: default;