使用javascript更改textarea包装 [英] Changing textarea wrapping using javascript

查看:137
本文介绍了使用javascript更改textarea包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的小wiki应用程序,我主要需要使用textarea来编辑内容以使用软(或虚拟)包装。然而,在某些情况下,不包含内容将是优选的。我以为我会这样做,只需要一个按钮来关闭包装。以下是简化代码:

For my small wiki application, I mostly need to have the textarea used to edit the contents to use soft (or virtual) wrapping. However, in some cases, not wrapping the content would be preferable. I thought I would do this by simply having a button to turn off wrapping. Here is the simplified code:

  <form name="wikiedit" action="[[script_name]]" method="post">
    <textarea name="content" rows="25" cols="90" wrap="virtual">[[content]]</textarea>
    <input type="button" onclick="document.wikiedit.content.wrap='off';" value="No Wrap"> &nbsp;
    <input type="submit" value="Save">
  </form>

它适用于IE,但不适用于Firefox或Opera。我应该怎么做?

It works with IE, but not with Firefox or Opera. How should I do this?

推荐答案

查看错误41464: https://bugzilla.mozilla.org/show_bug.cgi?id=41464

See bug 41464: https://bugzilla.mozilla.org/show_bug.cgi?id=41464

现在的讨厌的解决方法是用自己的克隆替换textarea:

Nasty workaround for now is to replace the textarea with a clone of itself:

function setWrap(area, wrap) {
    if (area.wrap) {
        area.wrap= wrap;
    } else { // wrap attribute not supported - try Mozilla workaround
        area.setAttribute('wrap', wrap);
        var newarea= area.cloneNode(true);
        newarea.value= area.value;
        area.parentNode.replaceChild(newarea, area);
    }
}

无关:尽量避免直接从文档对象,在某些浏览器上是不可靠的,并导致名称冲突问题。 'document.forms.wikiedit'更好,并转移到表单上的'id'而不是'name',然后使用'document.getElementById('wikiedit')'更好地。

Unrelated: try to avoid accessing elements straight out of the document object, it is unreliable on some browsers and causes name clash problems. ‘document.forms.wikiedit’ is better, and moving to ‘id’ on the form instead of ‘name’ and then using ‘document.getElementById('wikiedit')’ better still.

form.elements.content也比form.content更可靠,因为类似的原因...或者,您可以给textarea一个ID,并直接使用getElementById到textarea,而无需打扰表单。

form.elements.content is also more reliable than form.content for similar reasons... or, indeed, you could give the textarea an ID and go straight to the textarea with getElementById without having to bother look at the form.

这篇关于使用javascript更改textarea包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆