我正在寻找一种方法来消毒输入,我粘贴到浏览器,这是可能的用jQuery做吗?
到目前为止,我已经想出了这个:
$(this).live(pasteEventName, function(e) {
// this is where i would like to sanitize my input
return false;
}
不幸的是,我的发展因为这个“小”问题而戛然而止。 如果有人能给我指出正确的方向,我真的会让我成为一个快乐的露营者。
我正在寻找一种方法来消毒输入,我粘贴到浏览器,这是可能的用jQuery做吗?
到目前为止,我已经想出了这个:
$(this).live(pasteEventName, function(e) {
// this is where i would like to sanitize my input
return false;
}
不幸的是,我的发展因为这个“小”问题而戛然而止。 如果有人能给我指出正确的方向,我真的会让我成为一个快乐的露营者。
当前回答
这里有一个警告。在Firefox中,如果您重置每个键上的输入文本,如果文本长于输入宽度允许的可视区域,则重置每个键上的值将破坏浏览器自动将文本滚动到文本末尾的插入号位置的功能。相反,文本将滚动回开始,而插入符号则不在视图中。
function scroll(elementToBeScrolled)
{
//this will reset the scroll to the bottom of the viewable area.
elementToBeScrolled.topscroll = elementToBeScrolled.scrollheight;
}
其他回答
该方法使用jqueries contents().unwrap()。
First, detect the paste event Add a unique class to the tags that are already in the element into which we are pasting. After a given timeout scan through all the contents unwrapping tags that don't have the class that you set earlier. Note: This method does not remove self closing tags like See an example below. //find all children .find('*') and add the class .within .addClass("within") to all tags $('#answer_text').find('*').each(function () { $(this).addClass("within"); }); setTimeout(function() { $('#answer_text').find('*').each(function () { //if the current child does not have the specified class unwrap its contents $(this).not(".within").contents().unwrap(); }); }, 0);
监听粘贴事件并设置一个keyup事件监听器。在keyup上,捕获值并删除keyup事件监听器。
$('.inputTextArea').bind('paste', function (e){
$(e.target).keyup(getInput);
});
function getInput(e){
var inputText = $(e.target).val();
$(e.target).unbind('keyup');
}
这里有一个警告。在Firefox中,如果您重置每个键上的输入文本,如果文本长于输入宽度允许的可视区域,则重置每个键上的值将破坏浏览器自动将文本滚动到文本末尾的插入号位置的功能。相反,文本将滚动回开始,而插入符号则不在视图中。
function scroll(elementToBeScrolled)
{
//this will reset the scroll to the bottom of the viewable area.
elementToBeScrolled.topscroll = elementToBeScrolled.scrollheight;
}
好吧,正好碰到了同样的问题。我绕了很长的路
$('input').on('paste', function () {
var element = this;
setTimeout(function () {
var text = $(element).val();
// do something with text
}, 100);
});
只是一个小的超时,直到.val() func可以填充。
E.
事实证明,这是相当虚幻的。在粘贴事件函数内的代码执行之前,输入的值不会更新。我尝试从粘贴事件函数中调用其他事件,但输入值仍然没有与任何事件函数内的粘贴文本一起更新。这是除了keyup之外的所有事件。如果从粘贴事件函数中调用keyup,则可以从keyup事件函数中清除粘贴的文本。像这样…
$(':input').live
(
'input paste',
function(e)
{
$(this).keyup();
}
);
$(':input').live
(
'keyup',
function(e)
{
// sanitize pasted text here
}
);
这里有一个警告。在Firefox中,如果您重置每个键上的输入文本,如果文本长于输入宽度允许的可视区域,则重置每个键上的值将破坏浏览器自动将文本滚动到文本末尾的插入号位置的功能。相反,文本将滚动回开始,而插入符号则不在视图中。