简体   繁体   English

Google Wave:他们如何使div可点击

[英]google wave: how did they make divs clickable

As we are facing GWT performance issues in a mobile app I peeked into Google Wave code since it is developed with GWT. 当我们在移动应用程序中遇到GWT性能问题时,我偷看了Google Wave代码,因为它是由GWT开发的。

I thought that all the buttons there are widgets but if you look into generated HTML with firebug you see no onclick attribute set on clickable divs. 我以为所有按钮都有小部件,但是如果您用Firebug查看生成的HTML,则不会在可点击的div上设置onclick属性。 I wonder how they achieve it having an element that issues click or mousedown events and seemingly neither being a widget nor injected with onclick attribute. 我不知道他们如何实现它具有发出单击或鼠标按下事件的元素,并且看起来既不是小部件,也不注入onclick属性。

Being able to create such components would surely take me one step further to optimizing performance. 能够创建这样的组件肯定会使我进一步优化性能。

Thanks. 谢谢。 ps: wasnt google going to open source client code too. ps:不是Google也要开源客户端代码。 Have not been able to find it. 一直找不到。

You don't have to put an onclick attribute on the HTML to make it have an onclick handler. 您不必在HTML上放置onclick属性即可使其具有onclick处理程序。 This is a very simple example: 这是一个非常简单的示例:

<div id="mydiv">Regular old div</div>

Then in script: 然后在脚本中:

document.getElementById('mydiv').onclick = function() { 
    alert('hello!');
}

They wouldn't set the onclick property directly, it would have been set in the GWT code or via another Javascript library. 他们不会直接设置onclick属性,而是会在GWT代码中或通过其他Javascript库进行设置。

The GWT documentation shows how to create handlers within a GWT Java app: GWT文档显示了如何在GWT Java应用程序中创建处理程序:

public void anonClickHandlerExample() {
  Button b = new Button("Click Me");
  b.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
      // handle the click event
    }
  });
}

This will generate an HTML element and bind a click handler to it. 这将生成一个HTML元素并将点击处理程序绑定到该元素。 However, in practice this has the same result as using document.getElementById('element').onclick() on an existing element in your page. 但是,实际上,这与在页面中现有元素上使用document.getElementById('element').onclick()具有相同的结果。

You can hook functions to the onclick event using JavaScript. 您可以使用JavaScript将函数挂钩到onclick事件。 Here's an example using jQuery : 这是使用jQuery的示例:

$(document).ready(function(){
    $("#div-id").click(function(){
        /* Do something */
    });
});

如果您有兴趣优化此性能,则可能需要根据情况调查事件委派。

A click event is generated for every DOM element within the Body. 为主体中的每个DOM元素生成一个click事件。 The event travels from the Body down to the element clicked (unless you are using Internet Explorer), hits the element clicked, and then bubbles back up. 事件从“正文”向下传播到单击的元素(除非您使用的是Internet Explorer),然后单击单击的元素,然后重新冒泡。 The event can be captured either through DOM element attributes, event handlers in the javascript, or attributes at any of the parent levels (the bubbling or capturing event triggers this). 可以通过DOM元素属性,javascript中的事件处理程序或任何父级属性来捕获事件(冒泡或捕获事件会触发此事件)。

I'd imagine they've just set it in a .js file. 我以为他们只是将其设置为.js文件。 Easily done with say jQuery with $(document).ready() for example. 例如,使用$(document).ready()轻松地说出jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM