问题
A function is bound to an event for many objects. One of those objects fires the event and jQuery kicks in. How do I find out who fired the event inside the JavaScript function?
I've tried using function [name](event){}
and poking at event but I get "'data' is null or not an object".
Here's my code right now:
<script type="text/javascript">
$(document).ready(function() {
$('.opening').bind("click", function(event) {
//code to populate event.data since right now, the tags don't hold the info...they will eventually with an XHTML custom namespace
event.data.building = "Student Center";
event.data.room = "Pacific Ballroom A";
event.data.s_time = "9/15/2009 8:00";
event.data.e_time = "9/15/2009 10:00";
indicatePreferenceByClick(event);
});
function indicatePreferenceByClick(event) {
alert("I got clicked and all I got was this alert box.");
$("#left").load("../ReserveSpace/PreferenceByClick", { building: event.data.building, room: event.data.room, s_time: event.data.s_time, e_time: event.data.e_time});
};
</script>
回答1:
Using target
you can access the control that has caused the current execution/ event. Like this:
$(event.target)
回答2:
Here is a working example of a complete page. The caller is logged to the console in the line console.log($caller.prop("id"));
:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.opening').on("click", {building:"Student Center", room:"Pacific Ballroom A", s_time:"9/15/2009 8:00", e_time:"9/15/2009 10:00"}, function(event) {
indicatePreferenceByClick(event);
});
function indicatePreferenceByClick(event) {
alert("I got clicked and all I got was this alert box.");
var $caller = $(event.target);
console.log($caller.prop("id"));
var building = event.data.building;
var room = event.data.room;
var s_time = event.data.s_time;
var e_time = event.data.e_time;
};
});
</script>
</head>
<body>
<div style="padding:10px"><button type="button" id="button1" class="opening">button1</button></div>
<div style="padding:10px"><button type="button" id="button2" class="opening">button2</button></div>
</body>
</html>
回答3:
You don't need jQuery at all to get the target of an event. It's always available as the currentTarget property of the event. You can use jQuery to set up the event handler, of course, but getting the target is easily done without using that library. For example:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<button type="button" id="button1" class="opening">button1</button>
<button type="button" id="button2" class="opening">button2</button>
<script type="text/javascript">
$(document).ready(function() {
$('.opening').bind("click", function(event) {
indicatePreferenceByClick(event);
});
});
function indicatePreferenceByClick(event) {
console.log('target', event.currentTarget);
}
</script>
</body>
</html>
As for dealing with 'null' when trying to access event.data, that makes perfect sense because 'data' is not a property of a click event.
$(document).ready(function() {
$('.opening').bind("click", function(event) {
// event.data is null so you can't add properties to it
event.data.building = "Student Center"; // <- this will fail
indicatePreferenceByClick(event);
});
});
You can either add another property to the data object like so:
$(document).ready(function() {
$('.opening').bind("click", function(event) {
event.data = {};
event.data.building = "Student Center";
indicatePreferenceByClick(event);
});
});
Or, you can pass another variable to your function:
$(document).ready(function() {
$('.opening').bind("click", function(event) {
var data = {};
data.building = "Student Center";
indicatePreferenceByClick(event, data);
});
});
回答4:
when you attach the event to multiple objects the best way to identify which of the elements fired the event is using the this keyword. Example -
$('.opening').bind("click", function(event) {
var current = $(this); // will give you who fired the event.
//code to populate event.data since right now, the tags don't hold the info...they will eventually with an XHTML custom namespace
event.data.building = "Student Center";
event.data.room = "Pacific Ballroom A";
event.data.s_time = "9/15/2009 8:00";
event.data.e_time = "9/15/2009 10:00";
indicatePreferenceByClick(event);
});
let me know if this helps.
回答5:
You can use the this in Jquery function, it will give you the item which has injected event.
<script type="text/javascript">
$(document).ready(function() {
$('.opening').bind("click", function(event) {
console.log($(this));
});
});
</script>
来源:https://stackoverflow.com/questions/1375280/get-information-about-the-source-of-an-event-with-jquery