【问题标题】:How to make HTML text field readonly, but also scrollable?如何使 HTML 文本字段只读,但也可滚动?
【发布时间】:2012-01-08 15:14:57
【问题描述】:

我有一个只读的 HTML 文本字段。事情是这样的,也就是说该字段只有 100 像素宽。例如,我有一个句子没有显示在 100 像素中。因为它是只读的,所以它不能滚动。

换句话说。我怎样才能让该字段仍然不可编辑。但也要使不适合该字段的较长字符串可见吗?

谢谢!

【问题讨论】:

  • 字段是什么意思? jsfiddle.net/hbKBD 例如,文本框是可滚动的)))
  • 是的,就像你的例子。但是检查一下,确定这个东西是不可编辑的,这很好。但是你的一段字符串被截断了,因为文本字段太短而无法显示整个字符串。你如何解决这个问题?在您的示例中,我无法滚动等。
  • 你可以用鼠标滚动它...只需选择文本
  • 虽然可以用鼠标滚动,但需要考虑键盘的可访问性。非只读/禁用的文本字段可以通过键盘完全访问,但当它变为禁用/只读时就不再是这种情况了

标签: html field readonly scrollable


【解决方案1】:

您可以使用一些 JavaScript。除非你使用框架,否则它看起来很丑,因为它不是微不足道的。

JavaScript keypress 事件在按下某个键时触发,但不会为光标键触发(出于某种原因)。这非常方便,因为如果您使用 JavaScript 来阻止默认操作,您就会被排序。

理想情况下,这将是您所需要的:

// get all readonly inputs
var readOnlyInputs = document.querySelectorAll('input[readonly]');

// Function to fire when they're clicked
// We would use the focus handler but there is no focus event for readonly objects
function readOnlyClickHandler () {
    // make it not readonly
    this.removeAttribute('readonly');
}
// Function to run when they're blurred (no longer have a cursor
function readOnlyBlurHandler () {
    // make it readonly again
    this.setAttribute('readonly');
}
function readOnlyKeypressHandler (event) {
    // The user has just pressed a key, but we don't want the text to change
    // so we prevent the default action
    event.preventDefault();
}
// Now put it all together by attaching the functions to the events...

// We have to wrap the whole thing in a onload function.
// This is the simplest way of doing this...
document.addEventListener('load', function () {
    // First loop through the objects
    for (var i = 0; i < readOnlyInputs.length; i++) {
        // add a class so that CSS can style it as readonly
        readOnlyInputs[i].classList.add('readonly');
        // Add the functions to the events
        readOnlyInputs[i].addEventListener('click', readOnlyClickHandler);
        readOnlyInputs[i].addEventListener('blur', readOnlyBlurHandler);
        readOnlyInputs[i].addEventListener('keypress', readOnlyKeypressHandler);
    }
});

只需复制并粘贴它,它应该可以在 Firefox 或 Chrome 中正常工作。该代码符合标准,但 Internet Explorer 不符合。所以这在 IE 中不起作用(可能版本 9 和 10 除外……不确定)。此外,classList.add 位仅适用于一些最新版本的浏览器。所以我们必须改变这些位。首先我们将调整readOnlyKeypressHandler 函数,因为event.preventDefault() 并不适用于所有浏览器。

function readOnlyKeypressHandler (event) {
    if (event && event.preventDefault) {
        // This only runs in browsers where event.preventDefault exists,
        // so it won't throw an error
        event.preventDefault();
    }
    // Prevents the default in all other browsers
    return false;
}

现在更改classList 位。

    // add a class so that CSS can style it as readonly
    if (readOnlyInputs[i].classList) {
        readOnlyInputs[i].classList.add('readonly');
    } else {
        readOnlyInputs[i].className += ' readonly';
    }

令人讨厌的是,IE 也不支持 addEventListener,因此您需要创建一个函数来单独处理(将其添加到 for 循环上方)

function addEvent(element, eventName, fn) {
    if (element.addEventListener) {
        element.addEventListener(eventName, fn, false);
    } else if (element.attachEvent) {
        // IE requires onclick instead of click, onfocus instead of focus, etc.
        element.attachEvent('on' + eventName, fn);
    } else {
        // Much older browsers
        element['on' + eventName] = fn;
    }
}

然后更改添加事件位:

    addEvent(readOnlyInputs[i], 'click', readOnlyClickHandler);
    addEvent(readOnlyInputs[i], 'blur', readOnlyBlurHandler);
    addEvent(readOnlyInputs[i], 'keypress', readOnlyKeypressHandler);

并且给文档加载函数一个名字,而不是在addEventListener中调用它:

function docLoadHandler () {
    ...
}

并在最后调用它

addEvent(document, 'load', docLoadHandler);

一旦你这样做了,它应该可以在所有浏览器中工作。

现在只需使用 CSS 设置 readonly 类的样式,以消除浏览器中显示的轮廓:

.readonly:focus {
    outline: none;
    cursor: default;
}

【讨论】:

  • 此代码是否通过删除只读属性使只读字段在单击后可编辑?
  • 是的,但会阻止键盘输入(尽管不是 100% 稳健——例如,用户仍然可以粘贴)。这已经 9 岁了,所以现在短版本应该可以工作了!
【解决方案2】:

不要将 textarea 设为只读。这就是我所做的:

<textarea id="mytextarea" wrap="off" style="overflow:auto"></textarea>

,然后在你的 JavaScript 中,使用 jQuery:

$('#mytextarea').keypress(function(event){
    event.preventDefault();
});

【讨论】:

    【解决方案3】:

    如果http://jsfiddle.net/msmailbox/jBGne/ 这是你需要的,那么你可以用 div 试试这个。

    <div id="alo">sfbskdgksfbgkjsfngndflgndfgldfngldfgldfg</div>
    

    用css

    #alo
    {
        width:100px;
        overflow-x:scroll;
        overflow-y:hidden;
    }
    

    【讨论】:

      【解决方案4】:

      CSS 属性overflow 设置滚动行为,例如overflow: auto 仅在内容超出区域时显示滚动条。使用overflow: scroll,您每次都会获得滚动条。

      【讨论】:

      • 你说的是真的。但是从 UI 设计的角度来看,文本字段上的水平滚动条看起来很丑。有没有我可以做的事情,用户至少可以在文本字段内单击。并使用键盘上的箭头按钮左右滚动光标。但也让它不可编辑?
      • 为什么滚动条比非 UI 解决方案差?能够在没有栏的情况下滚动是违反直觉的。
      【解决方案5】:

      为您的文本容器 div 定义高度并使用 overflow:auto 类似于下面的 css 代码。

      .yoruclass{
      width:100px;
      height:100px;/* stop text to go beyond the define height */
      overflow:hidden;/* making text div scrollable */
      }
      

      【讨论】:

        【解决方案6】:

        使用文本区域而不是输入文本字段。你不能在文本字段中添加滚动

        &lt;textarea cols="50" rows="5" &gt;&lt;/textarea&gt;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-30
          • 1970-01-01
          • 1970-01-01
          • 2015-05-11
          • 2020-05-11
          • 2023-03-17
          • 2013-08-21
          • 2014-01-16
          相关资源
          最近更新 更多