jquery 个事件的布尔组合
Boolean combinations of jquery events
我想知道结合基本 jQuery 事件的最佳方式是什么。
例如,如果我想 运行
function(e) {
console.log(e.pageX)
}
当两者
$('selector').mousedown
和
$('selector').mousemove
或当
$('selector').mouseup
我知道
$('selector').on('mousemove 'mouseup', ...)
但这会使我的事件处理程序响应 mousemovev 或 mouseup,而无需在验证 'mousemove' 时验证鼠标是否按下。
是否有规范的方式来组合事件侦听器以反映所需的逻辑?
编辑:
对于我正在构建的应用程序,我试图在用户突出显示页面上的文本时不断更新表单的隐藏字段,因此需要事件组合。在我的示例中,我试图从项目的特定功能中抽象出来,以便问题的本质更加明显,并且更容易适应未来读者的需求。
试试这个代码。我想这接近你想要的。基本上我只在 mousedown 被触发时绑定一个 mousemove 函数。 mouseup 用于处理文本突出显示的结尾。祝你好运!
$(document).ready(function(){
$(document).mousedown(function(){
$(document).bind( "mousemove", function() {
console.log(window.getSelection().toString());
});
});
$(document).mouseup(function(){
$(document).unbind("mousemove");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Hello, world!</p>
我想知道结合基本 jQuery 事件的最佳方式是什么。
例如,如果我想 运行
function(e) {
console.log(e.pageX)
}
当两者
$('selector').mousedown
和
$('selector').mousemove
或当
$('selector').mouseup
我知道
$('selector').on('mousemove 'mouseup', ...)
但这会使我的事件处理程序响应 mousemovev 或 mouseup,而无需在验证 'mousemove' 时验证鼠标是否按下。
是否有规范的方式来组合事件侦听器以反映所需的逻辑?
编辑: 对于我正在构建的应用程序,我试图在用户突出显示页面上的文本时不断更新表单的隐藏字段,因此需要事件组合。在我的示例中,我试图从项目的特定功能中抽象出来,以便问题的本质更加明显,并且更容易适应未来读者的需求。
试试这个代码。我想这接近你想要的。基本上我只在 mousedown 被触发时绑定一个 mousemove 函数。 mouseup 用于处理文本突出显示的结尾。祝你好运!
$(document).ready(function(){
$(document).mousedown(function(){
$(document).bind( "mousemove", function() {
console.log(window.getSelection().toString());
});
});
$(document).mouseup(function(){
$(document).unbind("mousemove");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Hello, world!</p>