天天看點

input标簽中設定readonly屬性後光标顯示問題

IE、火狐浏覽器中,在HTML中,如果把一個<input>的readonly屬性設定為"readonly",表示這個表單元素不能編輯。但是,滑鼠點選之後,這個表單元素還是有光标存在的。

以下方法可以解決這個問題:

1.設定屬性 disabled="disabled",

<input type="text" id="id" style="width:250px;height:30px;" disabled="disabled"/>
           

在這種情況下,表單中的元素不可以被編輯,而且谷歌,IE浏覽器中input框中的文本可以被選取,但是火狐浏覽器中input框中的文本不可以被選取。

2.設定屬性 unselectable="on",

<input type="text" id="id" style="width:250px;height:30px;" readonly="readonly" unselectable="on"/>
           

這種情況主要是解決IE浏覽器中的光标出現問題。火狐浏覽器中不支援。

3.οnfοcus="this.blur()"

<body><input type="text" id="id" readonly="readonly" unselectable="on" onfocus="this.blur()"/></body>

<script>

    $(document).on('focus', 'input[readonly]', function () {
            this.blur();
    });

</script>
           

這種情況主要是解決火狐浏覽器中的光标出現問題。此方法不僅會讓光标消失,而且使框中的資訊變得無法擷取。

上面三種方法視情況而定,希望可以幫大家的忙。

繼續閱讀