2013-09-24 17 views
1

有在我的網頁問題與jQuery上點擊更換DIV類

<div class="results_list"> 
<div class="bg_color shadowy item_wrapper"> 
    <div class="not_shown">some text here...</div> 
    <div class="social_and_download"> 
     <div class="play_div"> 
      <a class="play" href="#">play</a> 
     </div> 
    </div> 
</div> 
<div class="bg_color shadowy item_wrapper"> 
    <div class="not_shown">other text text here...</div> 
    <div class="social_and_download"> 
     <div class="play_div"> 
      <a class="play" href="#">play</a> 
     </div> 
    </div> 
</div> 
</div> 

下面的配置,有許多div.item_wrapper元素,均具有一個div.not_shown的孩子,我想打這可見,通過改變它的類時點擊相應的a.play鏈接。我能想出的最好的是:

<script type="text/javascript"> 
$(document).ready(function() { 
    $("a.play").click(function() { 
     $(this).closest("div.item_wrapper").find("div.not_shown").addClass('shown').removeClass('not_shown'); 
    });   
}); 

和它不工作。你能告訴我我做錯了什麼嗎? 10x

+3

這裏可以正常工作http://jsfiddle.net/j08691/qb2D7/。您的div是否被動態添加到您的頁面? – j08691

+1

如果您使用[hide of jquery](http://api.jquery.com/hide/),該怎麼辦? – FxckDead

+0

如果您的HTML正在被動態添加使用,事件委派像這樣.. $('。results_list')。on('click','a.play',function(){});或$(document).on('click','a.play',function(){}); – Krishna

回答

0

好, 最初的問題解決了 - 它從一開始的確是工作... STUPID我放在JavaScript代碼之前,我插入jQuery的1.9.1.min.js文件到網頁..我認爲您的建議不管怎樣,這裏是代碼的樣子:

<script type="text/javascript"> 
$(document).ready(function() { 
    $('.item_wrapper').on('click', 'a.play', function() { 
     $(this).closest("div.item_wrapper").find("div.not_shown").removeClass('not_shown').addClass('shown'); 
     $(this).closest("div.item_wrapper").focus(); 
    });   
}); 

它實際上是在results_list加入二動態生成的item_wrapper DIV N - 我糾正了我的問題(對於jQuery腳本它使反正沒有區別)

非常感謝再次

附:現在我正努力將焦點放在我點擊播放的item_wrapper上,因爲一旦我這樣做,它總是會把我帶到第一個 - 如果您有任何想法,請留下評論。下面一行沒有帶來任何東西:

$(this).closest("div.item_wrapper").focus(); 
+0

我已經嘗試將tabindex添加到item_wrapper div這裏建議http://stackoverflow.com/questions/3777580/change-focus-into-particular-div,即使動態生成tabindex,但它仍然不會使用上面的代碼獲得焦點... – Adrian

0

可能先嚐試.removeClass然後.addClass幫助?

+0

謝謝,我想它更有意義的語義,首先刪除類,然後添加一個新的 – Adrian