我在添加一个类到我目前在Wordpres中使用的一些jQuery选项卡时遇到了问题。下面的代码创建了一个创建选项卡的快捷代码。我的问题是,如何将类添加到正在显示的选项卡中,即“活动”选项卡。我希望能够将这个类添加到正在显示的选项卡中,这样我就可以用CSS设置它的样式。这是我目前正在使用的代码,它可以查找,但它没有向活动选项卡添加任何类:
add_shortcode( 'tabgroup', 'jqtools_tab_group' );
function jqtools_tab_group( $atts, $content ){
$GLOBALS['tab_count'] = 0;
do_shortcode( $content );
if( is_array( $GLOBALS['tabs'] ) ){
foreach( $GLOBALS['tabs'] as $tab ){
$tabs[] = '<li><a class="" href="#">'.$tab['title'].'</a></li>';
$panes[] = '<div class="pane"><h3>'.$tab['title'].'</h3>'.$tab['content'].'</div>';
}
$return = "\n".'<!-- the tabs --><ul class="tabs">'.implode( "\n", $tabs ).'</ul>'."\n".'<!-- tab "panes" --><div class="panes">'.implode( "\n", $panes ).'</div>'."\n";
}
return $return;
}
add_shortcode( 'tab', 'jqtools_tab' );
function jqtools_tab( $atts, $content ){
extract(shortcode_atts(array(
'title' => 'Tab %d'
), $atts));
$x = $GLOBALS['tab_count'];
$GLOBALS['tabs'][$x] = array( 'title' => sprintf( $title, $GLOBALS['tab_count'] ), 'content' => $content );
$GLOBALS['tab_count']++;
}谢谢。
发布于 2011-11-05 00:03:46
如果你使用jquery/javascript来添加这个类,效果会更好。
如果您确定哪个选项卡在页面加载时处于活动状态,则可以通过jquery动态添加活动类。
$(document).ready(function() {
// That is if the first tab is the default active tab.
// Assuming the li tag is what you want to apply the 'active' class on
$('ul.tabs li:first').addClass('active');
// Manipulate Class on click event.
// Assuming the li tag is what you want to apply the 'active' class on
$('ul.li a').click(function() {
// Remove any 'active' class previously added.
$(this).parent().siblings().removeClass('active');
// Add 'active' class to the selected tab.
$(this).parent().addClass('active');
});
});希望这能有所帮助。
https://stackoverflow.com/questions/8003388
复制相似问题