简体   繁体   English

全局点击事件会阻止元素的点击事件

[英]Global click event blocks element's click event

This should happen 这应该发生
If the user clicks on one of the two input boxes, the default value should be removed. 如果用户单击两个输入框之一,则应删除默认值。 When the user clicks elswhere on the webpage and one text field is empty, it should be filled with the default value from the data-default attribute of the spefic element. 当用户单击网页上的elswhere并且一个文本字段为空时,应使用spefic元素的data-default属性中的默认值填充该字段。

This happens 有时候是这样的
When somebody clicks somewhere on the page and the field is empty, the field will be filled with the right value, but when somebody clicks in the field again the text isn't removed. 当有人单击页面上的某个位置并且该字段为空时,该字段将填充正确的值,但是当有人再次单击该字段时,不会删除文本。 It seems like the $(document) click event is blocking the $(".login-input") click event, because the $(".login-input") is working without the $(document) click event. 似乎$(document) click事件阻止了$(".login-input") click事件,因为$(".login-input")在没有$(document) click事件的情况下可以正常工作。

JSFiddle JSFiddle
A sample of my problem is provieded here: JSFiddle 这里提供了我的问题的一个示例: JSFiddle

Tank you for helping! 帮您加油!

When you click on the input, the script is working, but since the input is in the document, a click on the input is a click on the document aswell. 当您单击输入时,脚本正在工作,但是由于输入在文档中,因此单击输入也就是单击文档。 Both function will rune, document is the last one. 这两个功能都会符文,文件是最后一个。

That is called event bubblingand you need to stop propagation : 这称为事件冒泡,您需要停止传播:

$(document).ready(function () {
    $(".login-input").click(function (e) {
        e.stopPropagation()
        $(this).val("");
    });
});

Fiddle : http://jsfiddle.net/kLQW9/3/ 小提琴: http : //jsfiddle.net/kLQW9/3/

That's not at all how you solve placeholders, you do it like so : 这根本不是解决占位符的方式,您可以这样做:

$(document).ready(function () {
    $(".login-input").on({
        focus: function () {
            if (this.value == $(this).data('default')) this.value = '';
        },
        blur: function() {
            if (this.value == '') this.value = $(this).data('default');
        }
    });
});

FIDDLE 小提琴

Preferably you'd use the HTML5 placeholder attribute if really old browsers aren't an issue. 如果真正的旧浏览器不是问题,则最好使用HTML5占位符属性。

EDIT: 编辑:

if you decide to do both, check support for placeholders in the browser before applying the javascript : 如果您决定两者都做,请在应用javascript之前检查浏览器是否支持占位符:

var i = document.createElement('input'),
hasPlaceholders = 'placeholder' in i;

if (!hasPlaceholders) {
    // place the code above here, the condition will 
    // fail if placeholders aren't supported
}

Try below code 试试下面的代码

$(document).ready(function () {
    $(".login-input").click(function () {
        $(this).val("");

    });
});
$(document).ready(function () {

    $(".login-input").each(function () {
        if ($(this).val() === "") {
            $(this).val($(this).attr("data-default"));
        }
    });
    $(".login-input").blur(function () {
        if ($(this).val() === "") {
            $(this).val($(this).attr("data-default"));
        }
    });

});

Check fiddle 检查小提琴

Why not to use focus and blur events? 为什么不使用焦点和模糊事件?

$(document).ready(function () {
    $(".login-input").focus(function () {
        $(this).val("");
    });
});
$(document).ready(function () {    
    $(".login-input").blur(function () {
        if ($(this).val() === "") {
            $(this).val($(this).attr("data-default"));
        }
    });    
});

http://jsfiddle.net/kLQW9/5/ http://jsfiddle.net/kLQW9/5/

PS In yours, and this code, on focus all data fro input will be cleared. PS在您手中,此代码将专注于输入的所有数据。 If you need to clear only default text, add proper condition for that. 如果您只需要清除默认文本,请为此添加适当的条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM