在span中使用word创建类



嗨,我正试图根据项目的第一个标签向项目添加一个类。

$('.project_footer').ready(function() {
$('.project').addClass( $('.tag a').text());
});

然而,在文档中的每个project_footer中找到每个标签,并将其作为类应用于每个项目。我只想要第一个,并将其应用到单个项目。

任何建议吗?

编辑:

<div class="project blue">
<p>content content content</p>
<div class="project_footer">
    <span class="tags"><a href="/projects/allblue">Blue</a></span>
</div>
</div>

您需要同时使用parenteach的jQuery函数,如下所示:

$(function(){
  $('.project_footer').each(function() {
      $(this).parents('.project').addClass($(this).find('.tags a').text().toLowerCase());
  });
});

我刚刚创建了一个jsFiddle来显示它的工作:)http://jsfiddle.net/MA9CV/1

编辑

如果您有多个标签,将find('.tags a')更改为find('.tags:first-child a'),如下所示jsFiddle http://jsfiddle.net/MA9CV/2/

我想我们可能需要看更多的HTML来看看发生了什么,但可能在这个$('.project:first-child').addClass($('.tag a:first-child').text());的行中有一些东西是你正在寻找的

不确定为什么选择。page_footer?ready将在加载完所有DOM元素后触发,您的代码应该如下所示

$(function() { $('.project').addClass( $('.tag a').text()); });

最新更新