使用JavaScript将字符添加到textarea或插入位置的末尾?

时间:2014-02-18 17:56:19

标签: javascript jquery html textarea innerhtml

我试图通过获取textarea的VALUE并将innerHTML更改为值+ mychar来实现此目的。如果我没有在预加载的textarea中添加任何字符串,它会起作用,但之后它就不起作用(意味着它什么都不做)。为什么不在我向textarea添加新字符串后呢?我更希望以同样的方式使用。请避免使用jQuery,除非重要。
HTML

<button onclick = "AddLess()" class = "class">
    &lt;
    </button>
    <button onclick = "AddMore()" class = "class">
    &gt;
    </button>
    <button onclick = "AddExl()" class = "class">
    !
    </button>
    <button onclick = "AddQuote()" class = "class">
    "
    </button>
    <button onclick = "AddThing1()" class = "class">
    {
    </button>
    <button onclick = "AddThing2()" class = "class">
    }
    </button>
    <button onclick = "AddFin()" class = "class">
    /
    </button>
    <button onclick = "AddSingle()" class = "class">
    '
    </button>
    <button onclick = "AddPlus()" class = "class">
    +
    </button>


的JavaScript

function AddLess()
{
document.getElementById('code').innerHTML = document.getElementById('code').value + "<";
}
function AddMore()
{
document.getElementById('code').innerHTML = document.getElementById('code').value + ">";
}
function AddExl()
{
document.getElementById('code').innerHTML = document.getElementById('code').value + "!";
}
function AddQuote()
{
document.getElementById('code').innerHTML = document.getElementById('code').value + '"';
}
function AddThing1()
{
document.getElementById('code').innerHTML = document.getElementById('code').value + "{";
}
function AddThing2()
{
document.getElementById('code').innerHTML = document.getElementById('code').value + "}";
}
function AddFin()
{
document.getElementById('code').innerHTML = document.getElementById('code').value + "/";
}
function AddSingle()
{
document.getElementById('code').innerHTML = document.getElementById('code').value + "'";
}
function AddPlus()
{
document.getElementById('code').innerHTML = document.getElementById('code').value + "+";
}
function AddBracket1()
    {
        document.getElementById('code').innerHTML = document.getElementById('code').value + "(";
    }
function AddBracket2()
    {
document.getElementById('code').innerHTML = document.getElementById('code').value + ")";
    }
function AddDot()
    {
document.getElementById('code').innerHTML = document.getElementById('code').value + ".";
    }
function AddSemi()
    {
        document.getElementById('code').innerHTML = document.getElementById('code').value + ";";
    }

1 个答案:

答案 0 :(得分:0)

function _addChar(c)
{
  document.getElementById('code').value = document.getElementById('code').value + c;
}

适合我