我知道我们可以使用bind paste事件,如下所示:
$('#id').bind('paste', function(e) {
alert('pasting!')
});
但问题是,它会在粘贴的文本粘贴之前调用。我希望在右键单击后触发功能 - >粘贴在输入字段上粘贴的文本,以便我可以访问事件处理函数内的粘贴值。
.change()
事件也无济于事。目前我使用.keyup()
事件,因为我需要在输入字段中输入时显示剩余的字符数。
答案 0 :(得分:24)
有点黑客,但是:
$("#id").bind('paste', function(e) {
var ctl = $(this);
setTimeout(function() {
//Do whatever you want to $(ctl) here....
}, 100);
});
答案 1 :(得分:16)
为什么不使用“输入”事件?
$("#id").bind('input', function(e) {
var $this = $(this);
console.log($this.val());
});
答案 2 :(得分:3)
这将阻止用户使用键盘粘贴,处理或剪切:
$("#myField").keydown(function(event) {
var forbiddenKeys = new Array('c', 'x', 'v');
var keyCode = (event.keyCode) ? event.keyCode : event.which;
var isCtrl;
isCtrl = event.ctrlKey
if (isCtrl) {
for (i = 0; i < forbiddenKeys.length; i++) {
if (forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
return false;
}
}
}
return true;
});
这个会对鼠标事件做同样的事情:
$("#myField").bind("cut copy paste",function(event) {
event.preventDefault();
});
即使上述内容无法阻止右键点击,用户也无法粘贴,剪切或复制该字段。
要在活动结束后使用它,就像您想知道的那样,您必须使用JavaScript Timing Event
setTimeout(function() {
// your code goes here
}, 10);
答案 3 :(得分:0)
我有同样的问题,我选择通过javascript复制粘贴操作并改为使用该输出:
var getPostPasteText = function (element, pastedData) {
// get the highlighted text (if any) from the element
var selection = getSelection(element);
var selectionStart = selection.start;
var selectionEnd = selection.end;
// figure out what text is to the left and right of the highlighted text (if any)
var oldText = $(element).val();
var leftPiece = oldText.substr(0, selectionStart);
var rightPiece = oldText.substr(selectionEnd, oldText.length);
// compute what the new value of the element will be after the paste
// note behavior of paste is to REPLACE any highlighted text
return leftPiece + pastedData + rightPiece;
};
有关getSelection
功能的来源,请参阅IE's document.selection.createRange doesn't include leading or trailing blank lines。
答案 4 :(得分:0)
无需绑定:
import requests
from bs4 import BeautifulSoup
def get_data(url):
if not response.ok:
print('Server Responded: {}'.format(response.status_code))
else:
soup = BeautifulSoup(response.text, 'lxml')
return(soup)
def get_detail_data(soup):
try:
title = soup.find('h1', id='itemTitle').text.strip()
except:
title = ''
try:
p = soup.find('span', id='prcIsum').text.strip()
currency, price = p.split(' ')
except:
currency = ''
price = ''
try:
sold = soup.find('span', class_='vi-qtyS-hot-red').a.text.strip().split(' ')[0]
except:
sold = ''
data = {
'title' : title,
'currency' : currency,
'price' : price,
'total units sold' : sold
}
return data
def get_index_data(soup):
try:
links = soup.find_all('a', class_='s-item__link')
except:
links = []
urls = [item.get('href') for item in links]
return urls
def main():
url = 'https://www.ebay.com/sch/i.html?_nkw=mens+shoes&_sacat=0'
products = get_index_data(get_data(url))
for link in products:
data = get_detail_data(get_data(link))
if __name__ == '__main__':
main()