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媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06