简体   繁体   English

如何使用Jquery在输入元素中查找class属性是否存在

[英]How to find whether the class attribute is present or not in the input element using Jquery

I need to find the class name specified in the input attribute using jquery 我需要使用jquery查找在输入属性中指定的类名称

<Html>
<body>
<input type="text" class="name"/>
</body>
</Html>

here how i can find the class name using jquery 这是我如何使用jQuery查找类名

Try .attr() :- 试试.attr() :-

$("input[type='text']").attr('class');

DEMO 演示

 $('input').hasClass('name') ? alert('has class name') : alert('has no class name') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" class="name"/> 

use .hasClass 使用.hasClass

Description: Determine whether any of the matched elements are assigned the given class. 说明:确定是否为任何匹配的元素分配了给定的类。

Try this: 尝试这个:

 var className = $('.name').attr('class'); alert(className); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" class="name"/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM