从元素中获取类名称,然后选择具有相同类的所有元素

编程入门 行业动态 更新时间:2024-10-25 13:22:19
本文介绍了从元素中获取类名称,然后选择具有相同类的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 $('.classToClick').live("click", function () { $(this).attr('disabled', 'disabled'); $(this).val("lol"); $.post('post.php', { string: "lalala" }, function (data) { $(this).removeAttr('disabled'); }); })

此代码不起作用,因为在后期我们使用$(this).当我们将其更改为.classToClick时,它将起作用.

This code doesn't work, because in post we use $(this). When we change it to .classToClick, it works.

如何获得元素的类单击,以便我们可以像这样使用它?

How can we get the class of the element clicked so that we could use it like this?

$('.classToClick').live("click", function () { var className = $(this).name; $(this).attr('disabled', 'disabled'); $(this).val("lol"); $.post('post.php', { string: "lalala" }, function (data) { $(className).removeAttr('disabled'); }); })

是否可以做类似的事情?

Is it possible to do something similar?

推荐答案

单击后,您需要捕获类名,可以通过以下方式完成:

Upon clicking, you will need to capture the class name, which can be done via:

var className = $(this).attr('class'); //Alternatively, $(this).prop('class')

,当您稍后在$选择器中引用它时,需要包括句点以表示正在选择一个类,例如:

and when you later refer to it inside of the $ selector, you need to include the period to denote a class is being selected, as such:

$('.' + className).removeAttr('disabled');

代码:

$('.classToClick').live("click", function() { var className = $(this).attr('class'); $(this).attr('disabled', 'disabled').val("lol"); $.post('post.php', {string : "lalala"}, function(data) { $('.' + className).removeAttr('disabled'); }); })

工作示例

Working Example

更多推荐

从元素中获取类名称,然后选择具有相同类的所有元素

本文发布于:2023-10-30 18:59:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543719.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!