将代理事件处理程序添加到按钮单击 [英] Adding delegates events handlers to button click
问题描述
嗨
我是jquery和java脚本的绝对初学者。我正在尝试添加委托事件处理程序
这是我需要执行的任务,使用#user_list中的类.user_link锚定元素
我是委托事件使用#user_list表示li a.user_link。我有点困惑,我想也许这应该怎么做但不是我想的。
$( #li a.user_link user_list)。on( 单击, user_list,handleGetUser(event){
alert($( this )。text());
});
这是完成此任务的正确方法吗?
感谢任何人
( #li a.user_link user_list ).on( 点击, user_list,handleGetUser(event){
alert(
(此跨度>)文本())。
});
做正确的方法是什么?
谢谢你们
可以有不同的方法为事件添加处理程序,或设置唯一的处理程序。
首先,一些背景:
这是最简单的方法,当你设置一个处理程序时:someElement =
document .getElementById( someId); // 我不会检查您的选择器
// 没有看到你的HTML和规格
someElement.onclick =
功能(事件){ / * ... * / } // 您可以使用事件对象
// 或
someElement.onclick = function (){ / * ... * / }
您可以添加事件处理程序(称为事件监听器)并在同一元素(同一事件实例)上有多个处理程序:
someElement.addEventListener(跨度>click , function (){ / * 做点什么* / });
参见: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener [ ^ ]。
现在,你可能更好地理解jQuery的作用: https:// api.jquery.com/on 。
也就是说,
someElement.on ( 点击, function () {
// 做点什么
});
在这个函数中,ar guments 选择器和数据是可选的,请参阅上面引用的on上的jQuery文档页面。
-SA
Hi
i am and absolute beginner to jquery and java script. I am trying to add delegate event handlers
This is the task that i am required to do, anchor elements with class .user_link from #user_list
the delegate events that i am using are #user_list for "li a.user_link". I am a bit confused and i thought that perhaps this how it should be done but it is not i guess.
$("#li a.user_link user_list").on("click", "user_list", handleGetUser(event){
alert($(this).text());
});
Whats the correct way of doing this task?
thanks anyways guys
("#li a.user_link user_list").on("click", "user_list", handleGetUser(event){ alert(
(this).text()); });
Whats the correct way of doing this task?
thanks anyways guys
There can be different ways to add a handler to an event, or to set the only handler.
First, some background:
This is the simplest way, when you just set a handler:someElement = document.getElementById("someId"); // I'm not going to check up your selectors // without seeing your HTML and the specs someElement.onclick = function(event) {/* ... */} // you can use event object // or someElement.onclick = function() {/* ... */}
You can add event handler (called event listener) and have more than one handler on the same element (same event instance):
someElement.addEventListener("click", function() {/* do something */});
See: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener[^].
Now, you might better understand what jQuery does: https://api.jquery.com/on.
That is,
someElement.on( "click", function() { // do something });
In this function, the arguments selector and data are optional, please see the jQuery documentation page on "on" referenced above.
—SA
这篇关于将代理事件处理程序添加到按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!