我认为我弄错了#34; $(' #groupdepID01> .lahatkami')" $(' ID> Class')
脚本
$('#groupdepID01 > .lahatkami').addClass('iamSelected');
HTML
<div id="groupdepID01">
<input class="lahatkami" id="01" />
</div>
<div id="groupdepID02">
<input class="lahatkami" id="02" />
</div>
<div id="groupdepID04">
<input class="lahatkami" id="03" />
</div>
预期输出应为
<div id="groupdepID01">
<input class="lahatkami iamSelected" id="01" />
</div>
<div id="groupdepID02">
<input class="lahatkami" id="02" />
</div>
<div id="groupdepID03">
<input class="lahatkami" id="03" />
</div>
答案 0 :(得分:1)
您的代码正常运行。
我怀疑你没有包含 jquery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
..
运行代码,然后选中Developer tools
或按F12
iamSelected 类内添加id =“groupdepID01”
现场演示 Here
下面的代码段示例
$('#groupdepID01 > .lahatkami').addClass('iamSelected');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="groupdepID01">
<input class="lahatkami" id="01" />
</div>
<div id="groupdepID02">
<input class="lahatkami" id="02" />
</div>
<div id="groupdepID04">
<input class="lahatkami" id="03" />
</div>
用于测试目的添加Css iamSelected
$('#groupdepID01 > .lahatkami').addClass('iamSelected');
.iamSelected
{
background-color:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="groupdepID01">
<input class="lahatkami" id="01" />
</div>
<div id="groupdepID02">
<input class="lahatkami" id="02" />
</div>
<div id="groupdepID04">
<input class="lahatkami" id="03" />
</div>
使用jquery-3.1.1.min.js
$('#groupdepID01 > .lahatkami').addClass('iamSelected');
.iamSelected
{
background-color:blue;
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<div id="groupdepID01">
<input class="lahatkami" id="01" />
</div>
<div id="groupdepID02">
<input class="lahatkami" id="02" />
</div>
<div id="groupdepID04">
<input class="lahatkami" id="03" />
</div>