官术网_书友最值得收藏!

Detecting element clicks

Having the ability to detect if a user has clicked on elements other than buttons can provide additional flexibility to your web application. You can attach click events to any HTML elements, just as we did with the buttons in the previous recipe.

Getting ready

To work through this recipe, we are first going to need a blank HTML page named recipe-2.html, the same as in the other recipes. Remember that you need to have the latest version of jQuery downloaded and easily accessible on your computer so that it can be included in recipe-2.html.

How to do it…

To understand how you can detect user clicks on elements other than buttons, perform the following steps:

  1. Add the following HTML to the recipe-2.html page you have just created. This HTML creates a very basic web page with an input, an anchor, and a division element.
    <!DOCTYPE html>
    <html>
    <head>
        <title>Chapter 2 :: jQuery Events</title>
        <script src="jquery.min.js"></script>
        <script>
    
        </script>
    </head>
    <body>
    <a href="#">Link 1</a>
    <input type="text" name="input1" />
    <div class="clickme">Click Me!</div>
    </body>
    </html>
  2. Within the script tags in the head tag of the HTML page we just created, add the following JavaScript code. This JavaScript code uses two different methods of attaching click event handlers to three DOM elements.
    $(function() {
        $('a').click(function(){
        alert("You have clicked a link!");
        });
        $('body').on('click', 'input[type="text"]', function(){
        alert("You have clicked a text input!");
        });
        $('.clickme').click(function(){
        alert("You have clicked a division element");
        });
    });
  3. Ensure that all the changes have been saved and then open recipe-2.html in a browser. When you click on any of the elements, you will be presented with a different JavaScript alert, demonstrating that each of these click events are being caught by the event handlers we created earlier in the recipe.

How it works…

We can select DOM elements using their tag names, such as a to select a link, and then use the .click() or .on() functions to attach a click event handler, as shown in the following code snippet. We can also use the CSS selector input[type="text"] to select all text inputs on the page.

$('.clickme').click(function(){
  alert("You have clicked a division element");
});

The preceding jQuery code attaches a click event to each DOM element with the .clickme class. These elements can be any DOM elements such as divs, buttons, links, inputs, and text areas. This gives the jQuery developer the flexibility to be able to interpret user interactions across all page elements.

Note

See the Detecting button clicks recipe of this chapter to understand the difference between .click() and .on() and why .on() is the preferred implementation.

See also

  • Detecting button clicks
  • Detecting key press events on inputs
主站蜘蛛池模板: 千阳县| 福建省| 兴国县| 分宜县| 保德县| 辛集市| 新丰县| 灵石县| 兴仁县| 清涧县| 麻阳| 靖安县| 富顺县| 贵南县| 鸡东县| 太仆寺旗| 丰镇市| 法库县| 民和| 南皮县| 清苑县| 淳化县| 武胜县| 大丰市| 阜阳市| 长治县| 华池县| 澄江县| 兰溪市| 鸡泽县| 宜春市| 昌平区| 朝阳区| 始兴县| 读书| 万源市| 崇明县| 紫阳县| 宜都市| 和硕县| 龙岩市|