JQuery中如何获得class的名字,使用attr("class").toString()对不对,还有没有更好的方法
var temp = "";
$.each($(".OnlyForInputSelectNormal"), function () {
if (temp == "") {
temp = $(this).val();
} else {
temp = temp + " , " + $(this).val();
}
if ( ($(this).parent().attr("class").toString()) == "MyName") {
temp = temp+"(Normal)";
}
});
<div class="MyName">
<asp:TextBox ID="TBVarName" runat="server" Width="30%" CssClass="OnlyForInputSelectNormal"></asp:TextBox>
</div>
var temp = "";
$.each($(".OnlyForInputSelectNormal"), function () {
if (temp == "") {
temp = $(this).val();
} else {
temp = temp + " , " + $(this).val();
}
if ( ($(this).parent().attr("class").toString()) == "MyName") {
temp = temp+"(Normal)";
}
});
<div class="MyName">
<asp:TextBox ID="TBVarName" runat="server" Width="30%" CssClass="OnlyForInputSelectNormal"></asp:TextBox>
</div>
<img src="test.jpg"/>jQuery 代码:
$("img").attr("src");结果:
test.jpg
$("div").attr("class");
temp = temp+"(Normal)";
} alert($("#show").attr("class").indexOf("b"));
alert($("#show2").attr("class") == "myCls");
alert($("div:eq(0)").attr("class") == "myCls"); <div id="show" class="a b c"></div>
<div id="show2" class="myCls"></div>
1.attr(name)
2.$(id)[index].className