gpt4 book ai didi

javascript - 需要 JavaScript 函数才能在 textarea 更改时运行

转载 作者:行者123 更新时间:2023-11-30 13:17:30 24 4
gpt4 key购买 nike

我有一个 <td>一旦我点击一个按钮,我就会变成一个文本区域。这很好用。我还有一个 characterCounterEdit 函数,它也可以工作。唯一的问题是字符计数器仅在我单击文本区域中的光标时才起作用。我想在进入 editCommentToggle() 时立即触发字符计数器功能。

JavaScript:

function editCommentToggle( id )
{
theRow = document.getElementById("id"+id);
//user = theRow.cells[0].innerHTML;
//date = theRow.cells[1].innerHTML;
com = theRow.cells[2].innerText ;


idx = 2;
maxlength = 250;

// Comment field
cell = theRow.cells[idx];
while( cell.childNodes.length > 0 ) cell.removeChild(cell.childNodes[0]);

spanTag = document.createElement("span");
spanTag.innerHTML = "You have <strong><span id='commentsCounter'>"+maxlength+"</span></strong> characters left.<br/>";
cell.appendChild(spanTag);
element = document.createElement("textarea");
element.id="commentsTextArea-"+id;
element.rows="3";
element.value = com;
element.style.width = "400px";
element.maxLength = "250";
element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounterEdit('commentsCounter', maxlength, this);};
cell.appendChild(element);

$(function()
{
setTimeout("syncCommentTableSizes()",0); <%-- Run after HTC code --%>
});

// Actions field
cell = theRow.cells[++idx];
while( cell.childNodes.length > 0 ) cell.removeChild(cell.childNodes[0]);

link = document.createElement("a");
link.href = 'javascript:saveComment('+id+')';

element = document.createElement( "img" );
element.className = "smallicon edit"; // check if we need this changed
element.src="../images/icon_save.gif";
element.border="0";
element.alt = "Save";

link.appendChild( element );

cell.appendChild(link);
cell.appendChild( document.createTextNode(" ") );

link = document.createElement("a");
link.href = 'javascript:cancelCommentEdit('+id+')';

element = document.createElement( "img" );
element.className = "smallicon delete"; // check if we need this changed
element.src="../images/icon_cancel.gif";
element.border="0";
element.alt = "Cancel";

link.appendChild(element);
cell.appendChild(link);
}



function characterCounterEdit(id, maxLen, inputElement)
{
spanId = document.getElementById(id);

if (spanId)
{
// Update the counter the user sees.
var whatIsLeft = maxLen - inputElement.value.length;

if ( whatIsLeft < 0 ) whatIsLeft = 0;
spanId.innerText = whatIsLeft;
}

// Restrict user from entering more than the maxlen.
if ( inputElement.value.length > maxLen )
{
inputElement.value = inputElement.value.substring( 0, maxLen );
}
}

最佳答案

为什么不直接在 editCommentToggle() 中调用所需的方法。您似乎暗示这就是您想要做的,但我不确定您为什么没有这样做?

function editCommentToggle( id )
{
//code to get the element here first as pointed out by a comment below.

characterCounterEdit('commentsCounter', maxlength, element);

.... rest of the function

}

关于javascript - 需要 JavaScript 函数才能在 textarea 更改时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11504975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com