如何使用javascript / jQuery访问类名列表? [英] How do I access the list of classnames using javascript/jQuery?
问题描述
这里是html元素:
< input class =myClass problemOnetype = text value = something><<<< ; /输入>
< input class =myClass problemTwotype = text value = somethingelse>< / input>
这里是我尝试访问classaname的javascript和jquery,既不是方法,也不是b工作...classname为空或不是对象。
var myElems = $(this).find(。myClass);
$ b $ myElems.each(function(index){
var classLista = $(this).className.split(/ \s + /);
var classListb = myElems [index ] .className.split(/ \s + /);
var classListc = this.className.split(/ \s + /);
categories [index] .key = classlist [ 1];
categories [index] .value = $(this).value();
});
最终目标是让problemOne和problemTwo作为字符串返回,然后存储在一个数组中,然后去数据库等等等等。
非常感谢您的任何输入!
编辑
解释了最终目标
您可以使用jQuery的 .attr()
方法来获取任何属性,包括类。
$(this).find('。myClass')。each(function(index){
var element = $(this),classList ;
classList = element.attr('class')。split(/ \s + /);
categories [index] = {
key:classList [1],
value:element.value()
}
});
另外: $。extend()
$。extend(categories [index],{
key:classList [1],
value:element.value()
} );
here are the html elements :
<input class="myClass problemOne" type=text value=something></input>
<input class="myClass problemTwo" type=text value=somethingelse></input>
and here is the javascript and jquery in which I am tryign to access classaname, neither method a, or b works ... "classname is null or not an object".
var myElems = $(this).find(".myClass");
myElems.each(function(index) {
var classLista = $(this).className.split(/\s+/);
var classListb = myElems[index].className.split(/\s+/);
var classListc = this.className.split(/\s+/);
categories[index].key = classlist[1];
categories[index].value = $(this).value();
});
The end goal is to have problemOne and problemTwo returned as string and then stored in an array, which then go to the database blah blah blah.
Thanks a lot for any input !
edit explained end goal
You can use jQuery's .attr()
method to get any attribute, including classes.
$(this).find('.myClass').each(function(index) {
var element = $(this), classList;
classList = element.attr('class').split(/\s+/);
categories[index] = {
key : classList[1],
value : element.value()
}
});
Aside:
if categories[index]
already contains other info, you could use $.extend()
$.extend(categories[index], {
key : classList[1],
value : element.value()
});
这篇关于如何使用javascript / jQuery访问类名列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!