将文本区域名称添加到 document.form

发布于 2024-12-21 03:37:14 字数 1038 浏览 1 评论 0原文

以下是不起作用的功能...当尝试将“newTextArea”添加到“document.postform.nTextArea”时

有人可以帮助我吗?

function AddText(text, newTextArea) {

    nTextArea = newTextArea;


    var tarea = document.postform.nTextArea;
    alert(nTextArea);
    if (typeof tarea.selectionStart != 'undefined'){ // if it supports DOM2
        start = tarea.selectionStart;
        end = tarea.selectionEnd;
        tarea.value = tarea.value.substr(0,tarea.selectionStart)
            + text + tarea.value.substr(tarea.selectionEnd);
        tarea.focus();
        tarea.selectionStart = ((start - end) == 0) ? start + text.length : start;
        tarea.selectionEnd = start + text.length;
    } else {
        if (tarea.createTextRange && tarea.caretPos) {
            var caretPos = tarea.caretPos;
            caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?   text + ' ' : text;
        }
        else {
            tarea.value += text;
        }
        tarea.focus(caretPos);
    }
}

Below is the function which dont work... when try to add "newTextArea" to "document.postform.nTextArea"

Can anyone help me?

function AddText(text, newTextArea) {

    nTextArea = newTextArea;


    var tarea = document.postform.nTextArea;
    alert(nTextArea);
    if (typeof tarea.selectionStart != 'undefined'){ // if it supports DOM2
        start = tarea.selectionStart;
        end = tarea.selectionEnd;
        tarea.value = tarea.value.substr(0,tarea.selectionStart)
            + text + tarea.value.substr(tarea.selectionEnd);
        tarea.focus();
        tarea.selectionStart = ((start - end) == 0) ? start + text.length : start;
        tarea.selectionEnd = start + text.length;
    } else {
        if (tarea.createTextRange && tarea.caretPos) {
            var caretPos = tarea.caretPos;
            caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?   text + ' ' : text;
        }
        else {
            tarea.value += text;
        }
        tarea.focus(caretPos);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2024-12-28 03:37:14

假设 newTextArea 是 textarea 元素的名称,访问它的正确方法是使用以下代码:

var tarea = document.postform.elements[newTextArea];
if (typeof tarea.selectionStart != 'undefined'){ // if it supports DOM2
    //...
}

Assuming newTextArea is name of textarea element, the correct way to access it is with such code:

var tarea = document.postform.elements[newTextArea];
if (typeof tarea.selectionStart != 'undefined'){ // if it supports DOM2
    //...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文