Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JQuery更改事件优先级_Javascript_Jquery_Html_Events_Javascript Events - Fatal编程技术网

Javascript 使用JQuery更改事件优先级

Javascript 使用JQuery更改事件优先级,javascript,jquery,html,events,javascript-events,Javascript,Jquery,Html,Events,Javascript Events,我有以下html代码: <input type="text" id="theInput" value=""/> <a href="#" id="theLink">Click me</a> 但是,由于Javascript事件优先级规则,当输入中的值发生更改时,change总是在单击之前执行,因此只显示“change”消息 我希望它仅在输入值更改且用户在任何其他位置(而不是链接)单击退出输入时才显示change。在最后一种情况下,我希望显示单击 例子是

我有以下html代码:

<input type="text" id="theInput" value=""/>     
<a href="#" id="theLink">Click me</a>
但是,由于Javascript事件优先级规则,当输入中的值发生更改时,
change
总是在单击之前执行,因此只显示“change”消息

我希望它仅在输入值更改且用户在任何其他位置(而不是链接)单击退出输入时才显示
change
。在最后一种情况下,我希望显示
单击

例子是


我使用jQuery 1.6.4。

好的,现在我知道了,你可以做了

$('#theLink').live('click', function(e){
    alert('click');
});

$('#theInput').live('change', function(e){
    //Check if the change events is triggerede by the link
    if(e.originalEvent.explicitOriginalTarget.data === "Click me"){
         //if this is the case trigger the click event of the link
         $('#theLink').trigger("click");
    }else{
        //otherwise do what you would do in the change handler
        alert('change');
    }
});

在这里拉小提琴

为什么不选择输入框的值。您必须将输入框的初始值存储在就绪函数中

 initialvalue= $('#theInput').val();
然后比较数值

 $('#theLink').live('click', function(){
    var newvalue =$('#theInput').val();
        if(newvalue!=initialvalue) {
            //do something
         }

  });

两个事件都将触发,但在您的示例中,当发生
onmousedown
事件时触发的
onchange
事件处理程序中的警报将停止触发
onmousedup
事件所需的
onmousedup
事件。使用
console.log
将显示这两个事件


据我所知,在每个浏览器中的
模糊
更改
事件之后,会触发
单击
事件(请查看)。
blur
change
的顺序在不同浏览器中有所不同(来源:)

要解决您的问题,您可以收听文档上的
单击
事件,并将事件的目标与链接
进行比较。任何单击事件都将出现在文档中(除非阻止)

试试这个:

var lastValue = '';

$(document).click(function(event) {
    var newValue = $('#theInput').val();

    if ($(event.target).is('#theLink')) {
        // The link was clicked
    } else if (newValue !== lastValue) {
        // Something else was clicked & input has changed
    } else {
        // Something else was clicked but input didn't change
    }

    lastValue  = newValue;
});

jshiddle:

问得好!我对这里的答案很感兴趣…我更新了我的答案,我终于理解了你的问题,你的代码我没有收到
change
消息,当值更改并且用户在链接外单击时。此外,我想要的是,当用户点击链接时,我不关心值的变化。谢谢你的回答。但是,我得到一个错误:
无法读取未定义的属性“数据”
好,现在我明白了。之前的错误发生在使用Chrome浏览器时,但适用于Firefox。这是一个很好的解决方案,但仅适用于基于Gecko的浏览器。Source:似乎有效,但在捕获页面中的所有
点击
事件时,我有点担心性能。@Pablo:用户点击太多是否会产生明显的影响?好的,我最终选择了类似于您的解决方案。非常感谢你。JSFIDLE的最终解决方案是以防它对任何人都有帮助。
var lastValue = '';

$(document).click(function(event) {
    var newValue = $('#theInput').val();

    if ($(event.target).is('#theLink')) {
        // The link was clicked
    } else if (newValue !== lastValue) {
        // Something else was clicked & input has changed
    } else {
        // Something else was clicked but input didn't change
    }

    lastValue  = newValue;
});