在具有特定类的'li'元素之前添加动态的'a'标签 [英] Prepending a dynamic 'a' tag to the 'li' elements with a certain class

查看:53
本文介绍了在具有特定类的'li'元素之前添加动态的'a'标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看以下代码:

<ul id="all-movies">                    
  <li class="movie">
    <img src="pic.jpg" />
  </li>
  <li class="movie featured">
    <img src="pic.jpg" />
  </li>
  <li class="movie featured">
    <img src="pic.jpg" />
  </li>
</ul>

在上面的代码中,我想在li元素的前面加上featured类,使<a href="<?php bloginfo('url'); ?>/hello"></a>像这样:

In the code above, I would like to prepend <a href="<?php bloginfo('url'); ?>/hello"></a> to the li elements with a class of featured so that it looks like this:

<ul id="all-movies">                    
  <li class="movie">
    <img src="pic.jpg" />
  </li>
  <li class="movie featured">
    <a href="<?php bloginfo('url'); ?>/hello"></a>
    <img src="pic.jpg" />
  </li>
  <li class="movie featured">
    <a href="<?php bloginfo('url'); ?>/hello"></a>
    <img src="pic.jpg" />
  </li>
</ul>

<?php bloginfo('url'); ?>将会替换为我的网站.

And <?php bloginfo('url'); ?> will be replaced with my site.

我该如何使用它?我已经尝试过类似以下的方法,但是没有用:

How can I get this to work? I have tried something like the following but it didn't work:

$("#all-movies li").hasClass('featured').prepend('<a href="<?php bloginfo('url'); ?>/hello"></a>');

注意:featured类是动态添加的.

Note: The featured class is being added dynamically.

-edit-这是我用来动态添加featured类的代码:

-edit- This is the code I'm using to add the featured class dynamically:

function wpse80098_filter_post_class( $classes ) {
    global $post;
    if ( 'yes' == get_post_meta( $post->ID, '_jsFeaturedPost', true ) ) {
        $classes[] = 'featured';
    }
    return $classes;
}
add_filter( 'post_class', 'wpse80098_filter_post_class' );

推荐答案

<script>
$(document).ready(function(){
    var yourURL = "<?php bloginfo('url'); ?>/hello";
    $("#all-movies li.featured").prepend('<a href="'+yourURL+'"></a>');
});
</script>

这篇关于在具有特定类的'li'元素之前添加动态的'a'标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆