Javascript 为什么jQuery更多/更少切换脚本获胜';你不在WordPress工作吗?
我正在尝试在我的文本中添加更多/更少的切换脚本,但它无法工作。 它应该在页面开始时显示25px的文本加载高度,以及全高,或者在按下jquery脚本的高度按钮后显示自动高度。 我无法让它在wordpress上工作 小提琴作品: 我尝试了两种不同的方法。 当然是用短代码Javascript 为什么jQuery更多/更少切换脚本获胜';你不在WordPress工作吗?,javascript,php,jquery,wordpress,function,Javascript,Php,Jquery,Wordpress,Function,我正在尝试在我的文本中添加更多/更少的切换脚本,但它无法工作。 它应该在页面开始时显示25px的文本加载高度,以及全高,或者在按下jquery脚本的高度按钮后显示自动高度。 我无法让它在wordpress上工作 小提琴作品: 我尝试了两种不同的方法。 当然是用短代码 function piki( $atts, $content = null ) { $url = '/?s='; $str = ''; $output = array(); $count = 0;
function piki( $atts, $content = null ) {
$url = '/?s=';
$str = '';
$output = array();
$count = 0;
foreach (explode(", ",$content) as $content) {
$count++;
$output[] = "<a href='" . $url . $content . "'>" . $content . "</a>";
if ($count == 50) {
break;
}
}
{ ?>
<script type="text/javascript">
jQuery('.more').click(function() {
if(jQuery('.piki').css('height') != '25px'){
jQuery('.piki').stop().animate({height: '25px'}, 200);
jQuery(this).text('More...');
}else{
jQuery('.piki').css({height:'100%'});
var xx = jQuery('.piki').height();
jQuery('.piki').css({height:'28px'});
jQuery('.piki').stop().animate({height: xx}, 400);
// ^^ The above is beacuse you can't animate css to 100% (or any percentage). So I change it to 100%, get the value, change it back, then animate it to the value. If you don't want animation, you can ditch all of it and just leave: jQuery('.show-more-snippet').css({height:'100%'});^^ //
jQuery(this).text('Less...');
}
});
</script>
<div id="piki" class="piki" style="height: 25px; overflow: hidden;"><?php echo implode(", ", $output); ?></div><a class="more" style="float: left;" href="#stay_here">Read more <br><br></a>
<?php }
}
add_shortcode( 'piki', 'piki' );
函数piki($atts,$content=null){
$url='/?s=';
$str='';
$output=array();
$count=0;
foreach(分解(“,”,$content)为$content){
$count++;
$output[]=“”;
如果($count==50){
打破
}
}
{ ?>
jQuery('.more')。单击(函数(){
if(jQuery('.piki').css('height')!='25px'){
jQuery('.piki').stop().animate({height:'25px'},200);
jQuery(this.text('More…');
}否则{
jQuery('.piki').css({height:'100%});
var xx=jQuery('.piki').height();
jQuery('.piki').css({height:'28px'});
jQuery('.piki').stop().animate({height:xx},400);
//^^以上是因为您无法将css动画设置为100%(或任何百分比)。因此我将其更改为100%,获取值,将其更改回,然后将其设置为值。如果您不需要动画,可以放弃所有动画,只需离开:jQuery('.show more snippet')。css({height:'100%});^//
jQuery(this.text('Less…');
}
});
jQuery('.more')。单击(函数(){
if(jQuery('.piki').css('height')!='25px'){
jQuery('.piki').stop().animate({height:'25px'},200);
jQuery(this.text('More…');
}否则{
jQuery('.piki').css({height:'100%});
var xx=jQuery('.piki').height();
jQuery('.piki').css({height:'28px'});
jQuery('.piki').stop().animate({height:xx},400);
//^^以上是因为您无法将css动画设置为100%(或任何百分比)。因此我将其更改为100%,获取值,将其更改回,然后将其设置为值。如果您不需要动画,可以放弃所有动画,只需离开:jQuery('.show more snippet')。css({height:'100%});^//
jQuery(this.text('Less…');
}
});
你的fiddle工作正常,那么你的WP站点上的错误是什么呢?我想我已经解决了,javascript应该在div之后加载。你可以通过将标记的内容包装在jQuery(document).ready(function(){…})块中来解决这个问题。然后,它将在整个页面加载并被解析后运行。
function piki( $atts, $content = null ) {
$url = '/?s=';
$str = '';
$output = array();
$count = 0;
foreach (explode(", ",$content) as $content) {
$count++;
$output[] = "<a href='" . $url . $content . "'>" . $content . "</a>";
if ($count == 50) {
break;
}
}
{ ?>
<script type="text/javascript">
jQuery('.more').click(function() {
if(jQuery('.piki').css('height') != '25px'){
jQuery('.piki').stop().animate({height: '25px'}, 200);
jQuery(this).text('More...');
}else{
jQuery('.piki').css({height:'100%'});
var xx = jQuery('.piki').height();
jQuery('.piki').css({height:'28px'});
jQuery('.piki').stop().animate({height: xx}, 400);
// ^^ The above is beacuse you can't animate css to 100% (or any percentage). So I change it to 100%, get the value, change it back, then animate it to the value. If you don't want animation, you can ditch all of it and just leave: jQuery('.show-more-snippet').css({height:'100%'});^^ //
jQuery(this).text('Less...');
}
});
</script>
<?php }
return '<div id="piki" class="piki" style="height: 25px; overflow: hidden;">'.implode(", ", $output).'</div><a class="more" style="float: left;" href="#stay_here">Read more <br><br></a>';
}
add_shortcode( 'piki', 'piki' );