Events on dynamically added classes(动态添加的类上的事件)
问题描述
我有一个表单,其中按钮的类是动态变化的,具体取决于请求的来源.我正在尝试捕获 onclick 事件,但它不包含动态添加的类.我确保该类被添加到按钮中.我哪里出错了.
I have a form where the class of the button is dynamically changing, depending up where the request is coming from. I am trying to capture the onclick event, but it does not take in the dynamically added class. I made sure that the class is being added to the button. Where can I be going wrong.
片段 --
$('.button').removeClass('oldClass');
$('.button').addClass('newClass');
<button class = "button">Button</button>
点击--
$('.newClass').click(function(){
alert("test");
});
虽然按钮类已更改为 newClass,但代码未到达此事件事件.
The code does not get to this event event though the button class is changed to newClass.
谢谢
推荐答案
那是因为当你绑定点击事件时,元素有另一个类.
Thats cause when you bind the click event the element has another class.
试试这样的:
$('body').on('click', '.newClass', function () {
alert('test');
});
示例
这篇关于动态添加的类上的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:动态添加的类上的事件


基础教程推荐
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01