我有以下 jquery 代码:
function InitVoteContainer(voteContainer) {
var _userVote = voteContainer.children(".storeUserVote").val();
var _userObjectId = voteContainer.children(".storeObjectId").val();
var _userVoteKey = voteContainer.children(".storeVoteKey").val();
var _UserAuth = voteContainer.children(".storeUserAuth").val();
var _voteButtonUp = voteContainer.children("div[pid='btUp']");
var _voteButtonDown = voteContainer.children("div[pid='btDown']");
var upBtValue = 0;
var downBtValue = 0;
_voteButtonUp.attr("class", "upVote_inactive");
_voteButtonDown.attr("class", "downVote_inactive");
_voteButtonUp.prop("onclick", null);
_voteButtonDown.prop("onclick", null);
}
该代码将在 upVote 和 downVote 按钮上切换类。代码运行良好,但从未设置类?问题是为什么?
编辑1:
更新一些代码:
此 jquery 代码在文档准备就绪时运行
function InitVoteControls() {
$("#mainPostList").find(".voteCon").each(function () {
InitVoteContainer($(this));
});
}
这是 jquery 使用的 HTML:
<ul id="mainPostList" class="verticalList">
@foreach (var postViewModel in Model.Posts)
{
<li>
<div class="postContainer">
<div class="voteCon">
<input class="storeUserVote" type="hidden" value="@Model.UserVote" />
<input class="storeObjectId" type="hidden" value="@Model.ObjectId" />
<input class="storeVoteKey" type="hidden" value="@Model.VoteKey" />
<input class="storeUserAuth" type="hidden" value="@Request.IsAuthenticated" />
@if (!Request.IsAuthenticated)
{
<div pid="btUpVote" class="upVote"></div>
}
else
{
<div pid="btUpVote" class="upVote_inactive"></div>
}
<br style="clear:both;" />
<div class="voteCountBox">
@Html.Encode(Model.VoteBalance)
</div>
<br style="clear:both;" />
@if (!Request.IsAuthenticated)
{
<div pid="btDownVote" class="downVote"></div>
}
else
{
<div pid="btDownVote" class="downVote_inactive"></div>
}
</div>
</div>
</li>
}
</ul>