Javascript 注册除某个元素外的单击

Javascript 注册除某个元素外的单击,javascript,input,onblur,hints,Javascript,Input,Onblur,Hints,我有一个JavaScript函数,在用户填写表单时单击输入以显示提示时显示或隐藏“跨度”: function prepareInputsForHints() { var inputs = document.getElementsByTagName("input"); for (var i=0; i<inputs.length; i++){ // test to see if the hint span exists first if (inputs[i].parentNo

我有一个JavaScript函数,在用户填写表单时单击输入以显示提示时显示或隐藏“跨度”:

function prepareInputsForHints() {
var inputs = document.getElementsByTagName("input");

for (var i=0; i<inputs.length; i++){
    // test to see if the hint span exists first
    if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
        // the span exists!  on focus, show the hint
        inputs[i].onfocus = function () {
            this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
        }
        // when the cursor moves away from the field, hide the hint
        inputs[i].onblur = function () {
            this.parentNode.getElementsByTagName("span")[0].style.display = "none";
        }
    }
}
}
函数prepareInputsFrynts(){
var inputs=document.getElementsByTagName(“输入”);

对于(var i=0;i您可以使用一个布尔变量来测试用户是否将鼠标放在提示上,然后如果在隐藏提示时使用onblur而不是mouse

在您的循环中,类似这样的内容:

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

        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);
}
var inputs=document.getElementsByTagName(“输入”);

对于(var i=0;i这是新的工作代码:

$(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);
}
});
$(函数(准备输入){
var inputs=document.getElementsByTagName(“输入”);

对于(var i=0;我把它放在循环中,但跨度现在没有显示出来,尝试了几种变体,但我对它很陌生,也许你可以告诉我整个prepareInputsFrhints()是如何实现的函数应该看起来像这里的代码示例:然后给我一个链接,让它像我为您做的这个示例一样工作。这是请求的结果。我不建议您使用严格的javascript,因为这将失去浏览器兼容性,而且我看到您已经包含了jQuery 1.6.4(旧版本),因此我在示例中使用jQuery中的$.ready加载您的函数。我在网站中实现此功能时仍有一些问题,我需要一个if参数来指定输入[I]。nextElementSibling必须是一个spandid您尝试此条件吗?
span instanceof HtmlSpameElement