1

在我的网页中有以下配置

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

它不工作。你能告诉我我做错了什么吗?10倍

4

2 回答 2

0

好吧,最初的问题已经解决了——它确实从一开始就起作用了......愚蠢的我在将jquery-1.9.1.min.js文件插入页面之前放置了 javascript 代码......无论如何我考虑了你的建议,这就是如何代码如下:

<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 div 下动态生成item_wrapper div - 我在我的问题中更正了这一点(对于 jQuery 脚本它没有任何区别)

再次非常感谢

PS现在我正在努力将注意力集中在我点击播放的item_wrapper上,因为一旦我这样做了,它总是会把我带到第一个 - 如果你也有任何想法,请发表评论。下面一行什么也没带来:

 $(this).closest("div.item_wrapper").focus();
于 2013-09-25T07:39:51.293 回答
0

可能先尝试 .removeClass 然后 .addClass 帮助?

于 2013-09-25T01:56:47.507 回答