I want to know how can I get the class name of an element from this
reference. I have following HTML element
我想知道如何从此引用中获取元素的类名。我有以下HTML元素
<input type="checkbox" class="NotSelected @placeholderString" onchange="SaveCompairedOffers(this);">
jQuery code
function SaveCompairedOffers(obj) {
//Hiding irrelevant code from here
AddCompareOfferInCompareBox();
}
In the AddCompareOfferInCompareBox function I want to append class name of the object on compare-box
class.
在AddCompareOfferInCompareBox函数中,我想在compare-box类上追加对象的类名。
function AddCompareOfferInCompareBox()
{
var innerHtml = "";
$('.SelectedOffer').each(function ()
//append class name of current passed object to this div
innerHtml = innerHtml + '<div class="compare-box"></div>';
});
$(".AddCompareOfferByMe").html(innerHtml);
}
How can I do this?
我怎样才能做到这一点?
3 个解决方案
#1
1
The simplest way to this is vanilla javascript. In your function, where you want to reference this
, add the following code to assign your classes to a variable:
最简单的方法是vanilla javascript。在您要引用它的函数中,添加以下代码以将类分配给变量:
var classes = this.className
If you want to get an array of all classes, you can achieve this by using the split() function, like the following:
如果要获取所有类的数组,可以使用split()函数实现此目的,如下所示:
var classes = this.className.split(' ')
#2
1
I would recommend you to use data-*
prefixed attribute to store arbitrary data.
我建议你使用data- * prefixed属性来存储任意数据。
<input type="checkbox" class="NotSelected" data-str="@placeholderString" onchange="SaveCompairedOffers(this);">
This can be fetched using HTMLElement.dataset property
这可以使用HTMLElement.dataset属性获取
The HTMLElement.dataset property allows access, both in reading and writing mode, to all the custom data attributes (data-*) set on the element. It is a map of DOMString, one entry for each custom data attribute.
HTMLElement.dataset属性允许在读取和写入模式下访问元素上设置的所有自定义数据属性(data- *)。它是DOMString的映射,每个自定义数据属性都有一个条目。
function SaveCompairedOffers(obj){
var str = obj.dataset.str;
AddCompareOfferInCompareBox(str );
}
function AddCompareOfferInCompareBox(str) {
$('.SelectedOffer').each(function ()
$('<div></div>', {
"class" : "compare-box " + str
}).appentTo(".AddCompareOfferByMe");
});
}
However if you still want to use class name, then Element.className property can be used.
但是,如果您仍想使用类名,则可以使用Element.className属性。
className gets and sets the value of the class attribute of the specified element.
className获取并设置指定元素的class属性的值。
usage
function SaveCompairedOffers(obj){
var str = obj.className;
}
#3
0
To answer the actual question, how to get the css class assigned to an object from a jQuery reference to it, try this:
要回答实际问题,如何从jQuery引用中获取分配给对象的css类,请尝试以下方法:
var elementClass = ($(myselector).prop('className');
var elementClass =($(myselector).prop('className');
#1
1
The simplest way to this is vanilla javascript. In your function, where you want to reference this
, add the following code to assign your classes to a variable:
最简单的方法是vanilla javascript。在您要引用它的函数中,添加以下代码以将类分配给变量:
var classes = this.className
If you want to get an array of all classes, you can achieve this by using the split() function, like the following:
如果要获取所有类的数组,可以使用split()函数实现此目的,如下所示:
var classes = this.className.split(' ')
#2
1
I would recommend you to use data-*
prefixed attribute to store arbitrary data.
我建议你使用data- * prefixed属性来存储任意数据。
<input type="checkbox" class="NotSelected" data-str="@placeholderString" onchange="SaveCompairedOffers(this);">
This can be fetched using HTMLElement.dataset property
这可以使用HTMLElement.dataset属性获取
The HTMLElement.dataset property allows access, both in reading and writing mode, to all the custom data attributes (data-*) set on the element. It is a map of DOMString, one entry for each custom data attribute.
HTMLElement.dataset属性允许在读取和写入模式下访问元素上设置的所有自定义数据属性(data- *)。它是DOMString的映射,每个自定义数据属性都有一个条目。
function SaveCompairedOffers(obj){
var str = obj.dataset.str;
AddCompareOfferInCompareBox(str );
}
function AddCompareOfferInCompareBox(str) {
$('.SelectedOffer').each(function ()
$('<div></div>', {
"class" : "compare-box " + str
}).appentTo(".AddCompareOfferByMe");
});
}
However if you still want to use class name, then Element.className property can be used.
但是,如果您仍想使用类名,则可以使用Element.className属性。
className gets and sets the value of the class attribute of the specified element.
className获取并设置指定元素的class属性的值。
usage
function SaveCompairedOffers(obj){
var str = obj.className;
}
#3
0
To answer the actual question, how to get the css class assigned to an object from a jQuery reference to it, try this:
要回答实际问题,如何从jQuery引用中获取分配给对象的css类,请尝试以下方法:
var elementClass = ($(myselector).prop('className');
var elementClass =($(myselector).prop('className');