2012-01-21 36 views
2

我有一個jQuery的功能,當我單擊窗體上的輸入字段時,顯示/隱藏跨度看起來像「提示」的跨度。jQuery的技巧功能無法在基於webkit的瀏覽器中工作

功能上FirfFox,Chrome瀏覽器,IE(!):)等的偉大工程,但不是在所有基於WebKit瀏覽器Safari瀏覽器又名和Android(測試)

$(function(prepareInputsForHints) { 
var inputs = document.getElementsByTagName("input"); 
for (var i=0; i<inputs.length; i++){ 
    (function(i) { 
     // Let the code cleane 
     var span = inputs[i].nextElementSibling; 

    if(span instanceof HTMLSpanElement) { 

if(span.className == "hint") { 


     span.onmouseover = function() { this.isOver = true; } 
     span.onmouseout = function() { this.isOver = false; if(!inputs[i].isFocus) inputs[i].onblur(); } 

     // the span exists! on focus, show the hint 
     inputs[i].onfocus = function() { 
      this.isFocus = true; 
      span.style.display = "inline"; 
     } 
     // when the cursor moves away from the field, hide the hint 
     inputs[i].onblur = function() { 
      this.isFocus = false; 
      if(!span.isOver) span.style.display = "none"; 
     } 
}   
    } 
    })(i); 
} 
}); 

此外,爲方便起見,我爲您提供http://jsfiddle.net/eZnYY/1/

+0

在桌面版Chrome的代碼工作。 IE9僅支持? –

回答

1

嘗試更換你的代碼行:

if(span instanceof HTMLSpanElement) { 

與未來:

if(span && span.tagName.toUpperCase()==="SPAN") { 

http://jsfiddle.net/eZnYY/3/經過在桌面上Safary和Android瀏覽器(模擬器)

+0

它的工作原理,謝謝 –

1

您應該使用jQuery方法來確保跨瀏覽器兼容性(您的問題有jQuery標記)。

這是純粹的javascript。將您的代碼轉換爲jQuery,使用jQuery事件並設置css。

例如:

代碼

var inputs = document.getElementsByTagName("input"); 
for (var i=0; i<inputs.length; i++){ .. } 

jQuery的

$("input").each(function() { ... }); 
0

我不明白的是,如果你使用jQuery,你爲什麼沒有在你的功能中利用它?我向你保證,如果你使用jQuery來附加事件,他們將在webkit和所有其他瀏覽器中工作。

相關問題