javascript 如何从jQuery上的元素动态获取类名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20511449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get class name dynamically from element on jQuery?
提问by ralph
Is there a way to get the value of the classname like using an index or array? I have this code:
有没有办法像使用索引或数组一样获取类名的值?我有这个代码:
<input class="wpsc-buy-now-button wpsc-buy-now-button-64" type="image" name="submit" border="0" src="https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">
I want to get the wpsc-buy-now-button-64 dynamically. I don't want to use $(".wpsc-buy-button-64"), because my plan is to put it on a variable like so
我想动态获取 wpsc-buy-now-button-64。我不想使用 $(".wpsc-buy-button-64"),因为我的计划是把它放在一个像这样的变量上
var $classOfBtn = wpsc-buy-now-button-64;
64 is an id that I need to compare with another value in a foreach. This changes depending on content.
64 是一个 id,我需要与 foreach 中的另一个值进行比较。这取决于内容。
回答by Anil Saini
If you want to read the class of the input give it an id
and then use -
如果你想阅读输入的类,给它一个id
然后使用 -
var className = $('#id').attr('class');
and if you want to make the selector using class, you can store the class name to some variable as you have mentioned-
如果您想使用类制作选择器,您可以将类名存储到您提到的某个变量中 -
var $classOfBtn = wpsc-buy-now-button-64;
and then use it like this -
然后像这样使用它 -
$('.'+$classOfBtn)
回答by Ganesh Jadhav
HTML:
Give an ID to the control.
HTML:
给控件一个 ID。
<input id="btn" class="wpsc-buy-now-button wpsc-buy-now-button-64" type="image"
name="submit" border="0" src="https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif"
alt="PayPal - The safer, easier way to pay online" />
Javascript:
Javascript:
$(document).ready(function () {
var $classOfBtn = $('#btn').attr('class').split(' ')[0] + '-64';
alert($classOfBtn );
});
EDIT:
Provided your control has the name submit
, you can do this:
编辑:
如果您的控件具有 name submit
,您可以执行以下操作:
$(document).ready(function () {
var $allClasses = $("[name='submit']").attr('class').split(' ');
for(var i=0; i < $allClasses.length; i++)
{
// Check $allClasses[i] here.
}
});
Please see this working fiddle for my example: http://jsfiddle.net/jwyF8/
请参阅我的示例这个工作小提琴:http: //jsfiddle.net/jwyF8/
回答by ZenMaster
Assuming that this is what you would have;
假设这就是你所拥有的;
var $classOfBtn = 'wpsc-buy-now-button-64';
this would work just fine:
这将工作得很好:
$('.' + $classOfBtn) //note the dot
Update:
更新:
if you do indeed want to get all classes you'd have to assign some other mean of identifying your input
, so that it can be retrieved and "asked" for its classes list.
如果您确实想要获取所有类,则必须分配一些其他方法来识别您的input
,以便可以检索它并“询问”其类列表。
See here.
见这里。
回答by Edward Lombe
Substitute the string for a literal, and follow the instructions here:
将字符串替换为文字,并按照此处的说明进行操作:
It would also be helpful if you could describe what you mean by getting the button dynamically?
如果您可以通过动态获取按钮来描述您的意思,这也会有所帮助吗?
回答by Ringo
You can put all of classes in array like this:
您可以像这样将所有类放在数组中:
var classArray = $('input').attr('class').split(' ');
// So now you can declare variables
var classbtn1 = classArray[0];
var classbtn2 = classArray[1];