0
我有一個HTML代碼片段像下面設置HTML DIV類和設置DIV數據屬性在剃鬚刀使用jQuery查看
<div class="child-station-sub child-station-sub1">
<h4><a href='#' class="child-station-opener station1 open" data-staion="NORTH_SOUTH_LINE">North - South Line</a></h4>
<div class="child-station-sub child-station-sub2">
<h4><a href='#' class="child-station-opener station2 subdrop" data-staion="EAST_WEST_LINE">East - West Line</a></h4>
<div class="child-station-sub child-station-sub3">
<h4><a href='#' class="child-station-opener station3 subdrop" data-staion="COST_WEST_LINE">Cost - West Line</a></h4>
我試圖設置類和HTML數據上的屬性時,其裝載
於是,我就這樣做使用jQuery像下面
@foreach (var item in ViewBag.listGroups)
{
<div id="divstation" class="child-station-sub child-station-sub1">
<p class="hidden">@item.ID</p>
<h4><a id="stationlg" href='#' class="child-station-opener station1 open" data-staion="EAST_WEST_LINE">@item.GroupName</a></h4>
}
<script type="text/javascript">
$(document).ready(function() {
var idvalue = item.ID.ToString();
if (idvalue == 1) {
alert('sample')
$('#divstation').attr('class', 'child-station-sub child-station-sub1');
$('#stationlg').attr('class', 'child-station-opener station1 open');
$('#stationlg').attr('data-staion', "NORTH_SOUTH_LINE");
}
else if (idvalue == 2) {
$('#divstation').attr('class', 'child-station-sub child-station-sub2');
$('#stationlg').attr('class', 'child-station-opener station2 subdrop');
$('#stationlg').attr('data-staion', "EAST_WEST_LINE");
});
else if (idvalue == 3) {
$('#divstation').attr('class', 'child-station-sub child-station-sub3');
$('#stationlg').attr('class', 'child-station-opener station3 subdrop');
$('#stationlg').attr('data-staion', "COST_WEST_LINE");
});
</script>
但上面的嘗試不能正常工作,我該怎麼辦這裏
你是在一個循環中創建元素,因此它會創建重複的ID這將在HTML中渲染無效的HTML __Identifiers必須是唯一的_ – Satpal
這不是純HTML,這實際上是cshtml –
@kelumpriyadarshane它仍然會爲多個元素創建相同的ID,這是一個很大的禁忌。 – Carcigenicate