我有一个数组classNameList 我的HTML:
<ul id="servicepricing">
<li>
<a href="/service/page/servicepricing?dpt=4&ServiceModelListId27071" class="ServiceModelListId27071 servicepricingicon">FR-S Automatic</a>
<span style="display: none;">Scion FR-S Automatic Service Pricing</span>
</li>
<li>
<a href="/service/page/servicepricing?dpt=4&ServiceModelListId27072" class="ServiceModelListId27072 servicepricingicon">FR-S Manual</a>
<span style="display: none;">Scion FR-S Manual Service Pricing</span>
</li>
<li>
<a href="/service/page/servicepricing?dpt=4&ServiceModelListId27061" class="ServiceModelListId27061 servicepricingicon">iQ Automatic</a>
<span style="display: none;">Scion iQ Automatic Service Pricing</span>
</li>
<li>
<a href="/service/page/servicepricing?dpt=4&ServiceModelListId124" class="ServiceModelListId124 servicepricingicon">tC Automatic</a>
<span style="display: none;">Scion tC Automatic Service Pricing</span>
</li>
<li>
<a href="/service/page/servicepricing?dpt=4&ServiceModelListId125" class="ServiceModelListId125 servicepricingicon">tC Manual</a>
<span style="display: none;">Scion tC Manual Service Pricing</span>
</li>
</ul>
最初我隐藏了所有表格,当有人点击上面的锚点链接时,我必须显示具有相同类名的表格
<table id="FR-SManual" class="content sortable pureprice-data servicepricing-view ServiceModelListId27071" style="display: none;">
</table>
<table id="FR-SManual" class="content sortable pureprice-data servicepricing-view ServiceModelListId27072" style="display: none;">
</table>
<table id="FR-SManual" class="content sortable pureprice-data servicepricing-view ServiceModelListId27061" style="display: none;">
</table>
<table id="FR-SManual" class="content sortable pureprice-data servicepricing-view ServiceModelListId2124" style="display: none;">
</table>
<table id="FR-SManual" class="content sortable pureprice-data servicepricing-view ServiceModelListId125" style="display: none;">
</table>
var className,
classNameList = ["servicepricingicon", "main","ServiceModelListId27071"];
如何获取以&#34; ServiceModelListId&#34;开头的数组的值/ s。我试过用这个:
function fxnShowHide() {
var aElem = this,
classNameList = $(aElem).attr("class").split(' '),
tblwdhClassName;
if ($(aElem).hasClass('main')) {
$("#hero").show();
$("table.servicepricing-view").hide();
}
$.each(classNameList, function (key, value) {
if (value.indexOf("ServiceModelListId") === 0) {
tblwdhClassName = "table.servicepricing-view." + value;
}
});
$(tblwdhClassName).show();
}
$(".servicepricingicon").click(fxnShowHide);
是否有任何其他进程没有.each或for?
答案 0 :(得分:1)
if (value.substring(0, 7) == "Service") {
// ...
}
答案 1 :(得分:1)
var className,
classNameList = ["servicepricingicon", "main","ServiceModelListId27071"];
$.each( classNameList , function( key, value ) {
var anchor = $("." + classNameList[key]),
anchorText = anchor.text(),
anchorClass = anchor.attr("class");
if (anchorClass.indexOf("Service") > -1) {
className = anchorText;
alert(className)
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="servicepricing">
<li>
<a href="/service/page/servicepricing?dpt=4&ServiceModelListId27071" class="ServiceModelListId27071 servicepricingicon">FR-S Automatic</a>
<span style="display: none;">Scion FR-S Automatic Service Pricing</span>
</li>
<li>
<a href="/service/page/servicepricing?dpt=4&ServiceModelListId27072" class="ServiceModelListId27072 servicepricingicon">FR-S Manual</a>
<span style="display: none;">Scion FR-S Manual Service Pricing</span>
</li>
<li>
<a href="/service/page/servicepricing?dpt=4&ServiceModelListId27061" class="ServiceModelListId27061 servicepricingicon">iQ Automatic</a>
<span style="display: none;">Scion iQ Automatic Service Pricing</span>
</li>
<li>
<a href="/service/page/servicepricing?dpt=4&ServiceModelListId124" class="ServiceModelListId124 servicepricingicon">tC Automatic</a>
<span style="display: none;">Scion tC Automatic Service Pricing</span>
</li>
<li>
<a href="/service/page/servicepricing?dpt=4&ServiceModelListId125" class="ServiceModelListId125 servicepricingicon">tC Manual</a>
<span style="display: none;">Scion tC Manual Service Pricing</span>
</li>
</ul>
答案 2 :(得分:1)
尝试此操作:创建名为classsName
的数组,并使用.push()
为其添加值。您需要将indexOf
值与0
进行比较,以便将值检查为Service
var className = new Array();
$.each(classNameList, function( index, value ) {
if (value.indexOf("Service") == 0) {
className.push(value);
}
});
<强> JSFiddle Demo 强>
注意 - 从选择开始是区分大小写的
答案 3 :(得分:0)
我使用了$ .each功能
var classNameList = $(aElem).attr("class").split(' '),
elemClassName
$.each(classNameList, function (key, value) {
if (value.indexOf("ServiceModelListId") === 0) {
elemClassName = "." + value;
}
});