【问题标题】:.addClass on element in functions.php.addClass 在functions.php中的元素上
【发布时间】:2018-08-22 08:59:03
【问题描述】:

我不知道如何解决这个问题。在 header.php 我有这个 javascript

  <script language="javascript">
     $(document).ready(function() {
        $(".catfilter").click(function(){
           $("#category-post-content").addClass("shownewsajax");
           $(".sasasa").addClass("rrrrrrrrrrrr");
     });     
    }); 
    </script>    

它非常适用于#category-post-content,但由于“sasasa”元素在functions.php中(在add_action函数中),“rrrrrrrr”类不会在.catfilter点击时添加。

即使需要该类的元素在functions.php中,关于如何使其工作的任何想法?

这是functions.php中的代码,其中.sasasa是

///Ajax post filtering by category
 add_action( 'wp_ajax_load-filter', 'prefix_load_cat_posts' );
 function prefix_load_cat_posts () { ?>
 <div class="allposts row blogrow">
 <?php ob_start ();
 $query = new WP_Query(
    $args = array (
    'showposts' => 999,
    'cat' => $cat_id = $_POST[ 'cat' ]
 )  );
 $posts = get_posts( $args );
  foreach ( $posts as $post ) {
 setup_postdata( $post );
 ?>

<div class="sasasa news-wrap">  
<div class="newsflex">
<div class="postfeat"> <?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID ), 'my-custom-thumb' ); ?>
<img src="<?php echo $image[0]; ?>">
<?php endif; ?> 
 </div>
 <div class="postinfo">
 <div class="postcat">
 <?php foreach( (get_the_category($post)) as $category) { ?> <?php echo $category->cat_name; ?><?php } ?>
 </div>
 <h4 class="newstitle"><?php echo $post->post_title; ?></h4>
 <div class="postexc"><?php $excerpt = get_the_excerpt($post); echo   $excerpt;?></div>
 <a href="<?php echo get_permalink; ?>">READ MORE <span    class="rightarrow"></span></a>
 </div>
 </div>
  </div>


 <?php
 } wp_reset_postdata(); ?>
</div>
<div class="next-button"><a href="#" id="loadMoreCat">Load More Posts    CAT</a></div>

  <? $response = ob_get_contents();
  ob_end_clean();

   echo $response;
  die(1);
  } 

【问题讨论】:

  • 你应该用$(document).ready(function() {}):包装你的javascript
  • 你能给出设置 .sasasa 项的 HTML 代码吗?
  • 另外,我们不知道您如何包含“functions.php” - 因为这不是 PHP 问题,请单击 &lt;&gt; 并将 HTML 和脚本发布到 minimal reproducible example
  • Inazo 我加了代码,你能看一下吗?

标签: javascript php jquery wordpress


【解决方案1】:
$(document).ready(function() {
  $(".catfilter").click(function(){
      $("#category-post-content").addClass("shownewsajax");
      $(".sasasa").addClass("rrrrrrrrrrrr");
  });   
});

试试这个。这将在您页面中的所有内容都加载后执行。

【讨论】:

    【解决方案2】:

    .sasasa 元素是使用 Ajax 动态添加的。因此,当解析 jQuery 函数时,这些元素不存在。如果您从document.find() 那些元素开始,它应该可以工作。

    .catfilter 好像是静态的...

    <script language="javascript">
     $(document).ready(function() {
       $(".catfilter").click(function(){
         $("#category-post-content").addClass("shownewsajax");
         $(document).find(".sasasa").addClass("rrrrrrrrrrrr");
      });     
    }); 
    </script>  
    

    【讨论】:

    • 尝试使用一些控制台日志来调试它。例如console.log("There is "+$(document).find(".sasasa").length+" .sasasa elements.") 之类的...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 2021-04-30
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2017-10-21
    • 2019-02-04
    相关资源
    最近更新 更多