2012-12-29 32 views
0

我有以下的jquery代碼:爲什麼不用attr設置類?

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); 

} 

該代碼將接通給予好評和downVote按鈕類。代碼運行良好,但類從未設置?問題是爲什麼?

編輯1:

更新了一些更多的代碼:

在dodument準備

function InitVoteControls() { 

$("#mainPostList").find(".voteCon").each(function() { 
    InitVoteContainer($(this)); 
}); 
} 

這個jQuery代碼runnes這是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> 
+2

'$。attr('class',value)'工作正常。確保您的選擇不是空的。 – moonwave99

+1

很多時候人們並不認爲這個類正在被添加,但實際上這是他們的css,並不足以看到任何變化 – charlietfl

+0

添加了一些代碼,maby它是從voteContainer.children獲得的對象是不正確的?如何使用Chrome檢查此問題?我可以看到它確實找到了一個對象,但我不確定它是否正確。 – Ivy

回答

0

在JavaScript中,你與

voteContainer.children("div[pid='btUp']"); 

它會返回一個emtpy jQuery對象,因爲你具有確切屬性pid='btUp'您的標記沒有DIV選擇您的按鈕。您應該使用pid^='btUp',或直接使用pid='btUpVote'。 見this exampleW3C specification

1

您可以使用addClass

_voteButtonUp.addClass("upVote_inactive"); 
+0

是的,我知道,但那麼我將不得不使用removeClass以及我supose?我只想把班級換成簡單的屁股。不應該爲此工作嗎? – Ivy

+0

它應該已經工作,只要你的選擇不爲空嘗試'console.log(_voteButtonUp)'看看你想添加類的元素是否已被選中 – Rafay

0

這應該起作用了。 也許_voteButtonUp未定義。檢查你的選擇器。 但是,無論如何,使用addClass來做到這一點。

+0

我已經用更多的代碼更新了我的例子,看一看並看看你是否可以發現問題的請求。 – Ivy

0

您需要在jQuery中使用addClass()方法來實現您要做的結果。

_voteButtonUp.addClass("upVote_inactive"); 

另外,如果你使用CSS選擇巧妙,那麼你可以做.addClass( '無效');在JS代碼...

#buttonUp .inactive 
{ 
    /* style here */ 
} 
1

真的很難,因爲我們唐噸有HTML或如何調用InitVoteContainer來回答這個問題。但我可以向你保證.attr函數對'class'和value很有效。

我的第一個猜測是,voteContainer參數確定它不爲空,它的jQuery對象使用這個$(voteContainer)如果它不是一個jQuery對象。

第二個是HTML佈局,子功能只適用於元素的直接子元素,請檢查jQuery文檔。正如你可以從下面看到使用.find()可以是你的問題。

()方法從.find()在。兒童(不同)僅 行進的單一電平向下DOM樹而.find()可以遍歷 向下多個級別來選擇後代元素的。兒童(孫子, 等)。

+0

我添加了一些代碼(Edit1)。我也可以在調試中看到,我得到的div有類voteCon,所以我可能會得到錯誤的元素,我已經轉而發現沒有任何運氣。但即使有類voteCon的div元素,這個類也應該改變嗎? – Ivy

相關問題