jQuery的click()只能为DOM 0和通过jQuery注册的事件处理程序触发事件处理程序......? [英] jQuery's click() can trigger event handlers only for DOM 0 and event handlers registered through jQuery...?

查看:115
本文介绍了jQuery的click()只能为DOM 0和通过jQuery注册的事件处理程序触发事件处理程序......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[更新:]这个问题与jQuery的click()有什么关系?

jQuery的点击()没有点击?

以下代码:

  <div id="oneDiv">
    some content
  </div>

  <p>
    <a href="http://www.google.com" id="clickme">Click Me</a>
  </p>

        [ ... ]

  onload = function() { 

    $('#clickme').click(function() {
      $('#oneDiv').css({border: '6px dotted #07d'})
    });

    document.getElementById('clickme').onclick = function() {
      document.getElementById('oneDiv').style.color = 'green';
    }

    document.getElementById('clickme').addEventListener("click", function() {
      document.getElementById('oneDiv').style.background = '#ffc';
    }, false);  // bubbling phase


    setTimeout(function() {
      $('#clickme').click();
    }, 3000);

  }

如果点击链接,浏览器将

If you click on the link, then the browser will

1)将边框更改为6px点蓝色

2)将div内的文本更改为绿色

3)更改背景div to offwhite

4)转到www.google.com

1) change border to 6px dotted blue
2) change text inside the div to be green
3) change background of div to offwhite
4) go to www.google.com

但如果你等待并让setTimeout()的函数踢在,然后它只会执行

but if you wait and let the setTimeout()'s function to kick in, then it will only do the

$('#clickme').click(function() { })  
onclick = function() { ... }

它不会执行 addEventListener 一个,它不会跟随链接。 (顺便说一下,IE 8不允许 addEventListener

it will not do the addEventListener one, and it will not follow the link. (IE 8 won't allow addEventListener by the way)

这是真的吗? jQuery的click(),它与触发器('click')相同,只会触发通过自身注册的事件处理程序和DOM 0级事件处理程序?

So is it true? jQuery's click(), which is the same as trigger('click') will only fire off event handlers registered through itself and the DOM level 0 event handler?

推荐答案

jQuery应该为选择器和事件提供一些帮助,但是你可以将它与直接的javascript混合使用。我认为最好只使用jQuery格式:

jQuery is supposed to give you some help for selectors and events, but you mix it with straight javascript. I think it will be better to use jQuery format only so :

_replace

document.getElementById('clickme').onclick

by

$('#clickme').click(function {...});

_ replace

_ replace

addEventListener("click"

_ by

live("click") or bind("click")

(参见jQuery文档中的差异)。

(see jQuery documentation for the difference).

_最后:

document.getElementById('oneDiv').style.background = '#ffc';

_ by

$('#oneDiv').css('background-color','#ffc');

还:

更改onload by $(window).load(function(){....});

change onload by $(window).load(function() {....});

如果仍有问题请告诉我们,但我建议你如果你真的想进入jQuery并做强大的东西,请阅读一个很好的教程;)

Tell us after if there is still a problem, but I advise you you read a good tutorial if you really want to get into jQuery and do powerful stuff ;)

编辑:这绝对有效:

$(window).load(function() {
            $('#clickme').click(function() {
            $('#oneDiv').css({border: '6px dotted #07d'});
            $('#oneDiv').css('color','green');
            $('#oneDiv').css('background-color','#ffc');})

            setTimeout(function() {
                $('#clickme').click();
            }, 3000);

});

这篇关于jQuery的click()只能为DOM 0和通过jQuery注册的事件处理程序触发事件处理程序......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆