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');
});
示例
这篇关于动态添加的类上的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:动态添加的类上的事件
基础教程推荐
- Bokeh Div文本对齐 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
