duanche9384 2016-04-18 08:03
浏览 42
已采纳

wordpress用php或jQuery检查html类

I want to check the class of the html (there are posts and every category got a own class for background color) and then set the background. I wanted to to it with jQuery but it doesn't work because the first one is true so all get the same background.

My php code:

<div class="grid" >
<?php
  $args = array( 'post_type' => 'post' );
  $query = new WP_Query( $args );
  while ($query->have_posts()) : $query->the_post() ; ?>
    <div class="post grid-item <?php foreach((get_the_category()) as $category) { echo $category->cat_name; }  ?>" style='background:  url("<?php if (has_post_thumbnail()) { the_post_thumbnail_url(); }  ?>"); background-color:<?php  ?>' >
      <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    </div>
  <?php endwhile;
  wp_reset_query();
?>
</div>

my jQuery code that doesn't work

//post background
var back_engagement = jQuery('.grid div').hasClass('engagement');
var back_unternehmen =  jQuery('.grid div').hasClass('unternehmen');
var back_forum = jQuery('.grid div').hasClass('forum');

if (back_engagement){
  jQuery('.grid-item').css({"backgroundColor": "#959595"});
}
else if (back_unternehmen) {
  jQuery('.grid-item').css({"backgroundColor": "#96A306"});
}
else if (back_forum) {
  jQuery('.grid-item').css({"backgroundColor": "#215270"});
}

For me it doesn't mater if the solution is with php or jQuery

  • 写回答

1条回答

  • dongxing7530 2016-04-18 08:06
    关注

    You can set the CSS property based on simple selector.

    jQuery('.grid div.engagement').css({"backgroundColor": "#959595"});
    jQuery('.grid div.unternehmen').css({"backgroundColor": "#96A306"});
    jQuery('.grid div.forum').css({"backgroundColor": "#215270"});
    

    However I would recommend you to set them using classes in CSS

    .engagement{ background-color: #959595;}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 计算二重积分∫∫e^(x+y)dxdy,其中0≤x≤1,0≤y≤1,试分别用复合辛普森公式(取n=4)以及高斯求积公式(取n=4)计算积分 给出matlab程序
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的