简体   繁体   English

为什么此输入始终保持可点击状态?

[英]Why does this input always remain clickable?

Basically i want the outer div to take all events, so the input and anything else in the div is not clickable: 基本上我希望外部div占用所有事件,因此输入和div中的任何其他内容都不可点击:

<div id="div1">
    <div id="div2">
        <label>Input 1</label>
        <input type="text" id="input1" />
    </div>
</div>

$(document).ready(function() {
    $(document).on('click', '#input1', function(e){
        e.preventDefault();
    };
});

Here's the non-working example http://jsfiddle.net/KFWmk/3/ 这是非工作示例http://jsfiddle.net/KFWmk/3/

Any help appreciated :) 任何帮助赞赏:)

In newer jQuery (1.6+): 在较新的jQuery(1.6+)中:

$('div input').prop("readonly", true);

This sets the HTML readonly property to true for all inputs in a div. readonly div的所有输入的HTML readonly属性设置为true。

Please note that using .attr() to set properties (attributes with boolean values of on or off) has been deprecated in jQuery. 请注意,在jQuery中不推荐使用.attr()来设置属性(布尔值为on或off的属性)。

First of all bacause you havn't included the jQuery library. 首先,因为你没有包含jQuery库。
Second bacause you have a type-o. 第二个因为你有一个类型o。
Third because the the focus is emitted before the click event. 第三,因为焦点是在click事件之前发出的。 ( Prove ) 证明

If you want to make an input field not editable you can use: 如果要使输入字段不可编辑,可以使用:

$("input").prop("readonly", true);

or simply when you create the input field in html: 或者只是在html中创建输入字段时:

<input readonly type="number">

If you want to prevent focus on an input field you can use: 如果要防止对输入字段的关注,可以使用:

$("input").on("focus", function() {
    $(this).blur();
}); 
$('div input').attr("readonly", "readonly");

您可以将div中的所有输入字段设置为只读。

You can try like this 你可以这样试试

$(document).ready(function() {
    $('div input').prop("readonly", true);
});

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

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