javascript-events

Why is the target property of the mousewheel event different from that of other (click, mousedown, touchstart) events?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-13 09:09:11
问题 The mousewheel event's target property provides the DOM element that the mouse is currently hovering over as the mousewheel (or gesture-capable touchpad) is being operated. When I do this (at least in Safari 6, I will test other browsers later) I will get the text node itself as target . This never happens with other events which always produce a non text node even if I perform the action directly over text. Needless to say it makes the code more complex than otherwise. Is there a reason for

Why is the target property of the mousewheel event different from that of other (click, mousedown, touchstart) events?

与世无争的帅哥 提交于 2020-01-13 09:09:11
问题 The mousewheel event's target property provides the DOM element that the mouse is currently hovering over as the mousewheel (or gesture-capable touchpad) is being operated. When I do this (at least in Safari 6, I will test other browsers later) I will get the text node itself as target . This never happens with other events which always produce a non text node even if I perform the action directly over text. Needless to say it makes the code more complex than otherwise. Is there a reason for

Event propagation, overlay and drag-and-drop events

☆樱花仙子☆ 提交于 2020-01-13 08:06:10
问题 I want to overlay a div over the viewport when the user drags a file onto the window. However, I'm having trouble with the event propagation. When I set the overlay to display: block it appears to fire off a dragleave event and then another dragenter and then another dragleave again, so it's always in a post-dragleave state. Of course I call e.stopPropagation() and e.preventDefault() on the event object, but it doesn't seem to make a difference. The console.log() output when you drag

How to detect creation of new global variables?

心已入冬 提交于 2020-01-13 03:54:13
问题 I want watch for the creation of new global variables in Javascript so that, anytime a global variable is created, an event is fired. I've heard of the watch() function but that is only for watching for specific variable names. I want a catchall. 回答1: I don't know how to make this work "on demand" as soon as a var is created, but I can suggest a polling approach. In a browser window, all global become a member of the global "window" object. (Because technically, "window" is the "global object

Applying OOP with jQuery

十年热恋 提交于 2020-01-13 01:26:04
问题 I'm working with jQuery and trying to apply some basic Javascript OOP principles to a set of functions that control hover behavior. However, I can't figure out how to get the "this" keyword to refer to the instance of the object I'm creating. My sample code is: var zoomin = new Object(); zoomin = function() { // Constructor goes here }; zoomin.prototype = { hoverOn: function() { this.hoverReset(); // More logic here using jQuery's $(this)... }, hoverReset: function() { // Some logic here. } }

Applying OOP with jQuery

你说的曾经没有我的故事 提交于 2020-01-13 01:25:07
问题 I'm working with jQuery and trying to apply some basic Javascript OOP principles to a set of functions that control hover behavior. However, I can't figure out how to get the "this" keyword to refer to the instance of the object I'm creating. My sample code is: var zoomin = new Object(); zoomin = function() { // Constructor goes here }; zoomin.prototype = { hoverOn: function() { this.hoverReset(); // More logic here using jQuery's $(this)... }, hoverReset: function() { // Some logic here. } }

Behavior of removeEventListener

我与影子孤独终老i 提交于 2020-01-12 14:26:51
问题 Please check the below code, var clickfn = function(){ alert("clicked"); } document.getElementById("div1").addEventListener("click",clickfn,true); clickfn = function(){ }; document.getElementById("div1").removeEventListener("click"); http://jsfiddle.net/qUtzL/4/ Why does the removeEventListener does not work? Thanks! 回答1: removeEventListener takes 2 parameters, the event, and the function to remove. This should work: document.getElementById("div1").removeEventListener("click", clickfn); Also,

Behavior of removeEventListener

戏子无情 提交于 2020-01-12 14:26:42
问题 Please check the below code, var clickfn = function(){ alert("clicked"); } document.getElementById("div1").addEventListener("click",clickfn,true); clickfn = function(){ }; document.getElementById("div1").removeEventListener("click"); http://jsfiddle.net/qUtzL/4/ Why does the removeEventListener does not work? Thanks! 回答1: removeEventListener takes 2 parameters, the event, and the function to remove. This should work: document.getElementById("div1").removeEventListener("click", clickfn); Also,

Input Fires Keypress Event Twice

孤街醉人 提交于 2020-01-12 11:42:48
问题 This question has been asked/answered (mostly) before, BUT I've tried three things to stop the event from bubbling but nothing has worked: return false; e.stopPropagation(); e.preventDefault(); (return false should take care of the other two,correct?) Here's the html: <div class="tags-holder"> <input type="text" class="addField" id="addField_<%= visit.id %>" placeholder="add a new tag"> </div> And the JS (UPDATE CLEANED UP): $('.addField').show().keyup(function(event){ event.preventDefault();

Get value of List Item with jQuery

限于喜欢 提交于 2020-01-12 04:44:08
问题 How to get value and index of list item onClick event with jQuery? for example: <ul id='uItem'> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> </ul> 回答1: Combine the use of .index() and .text() (or .html(), if you wish): $('#uItem li').click(function() { var index = $(this).index(); var text = $(this).text(); alert('Index is: ' + index + ' and text is ' + text); }); 回答2: $('#uItem li').click(function(){ var $this = $(this); alert('Text ' + $this.text() + 'Index ' + $this