我正在寻找一种方法来消毒输入,我粘贴到浏览器,这是可能的用jQuery做吗?

到目前为止,我已经想出了这个:

$(this).live(pasteEventName, function(e) {
 // this is where i would like to sanitize my input
 return false;
}

不幸的是,我的发展因为这个“小”问题而戛然而止。 如果有人能给我指出正确的方向,我真的会让我成为一个快乐的露营者。


当前回答

$("#textboxid").on('input propertychange', function () {
    //perform operation
        });

它会工作得很好。

其他回答

这里有一个警告。在Firefox中,如果您重置每个键上的输入文本,如果文本长于输入宽度允许的可视区域,则重置每个键上的值将破坏浏览器自动将文本滚动到文本末尾的插入号位置的功能。相反,文本将滚动回开始,而插入符号则不在视图中。

function scroll(elementToBeScrolled) 
{
     //this will reset the scroll to the bottom of the viewable area. 
     elementToBeScrolled.topscroll = elementToBeScrolled.scrollheight;
}
 $('').bind('input propertychange', function() {....});                      

这将为鼠标粘贴事件工作。

实际上,您可以直接从事件中获取值。不过如何到达它有点迟钝。

如果你不想让它通过,则返回false。

$(this).on('paste', function(e) {

  var pasteData = e.originalEvent.clipboardData.getData('text')

});

嗯…我认为你可以使用e.clipboardData来捕捉被粘贴的数据。如果不成功,看看这里。

$(this).live("paste", function(e) {
    alert(e.clipboardData); // [object Clipboard]
});

为了跨平台兼容性,它应该处理oninput和onpropertychange事件:

$ (something).bind ("input propertychange", function (e) {
    // check for paste as in example above and
    // do something
})