我有一段代码,可以显示不同的服务器来观看视频
代码:
<div class="container-disable">
<ul class="tabs-ul nav-justified clearfix">
<?php $repeatable_fields = get_post_meta($post->ID, \'repeatable_fields\', true); if ( $repeatable_fields ) : ?>
<?php foreach ( $repeatable_fields as $field ) { ?>
<li>
<a href="javascript:void(0)" class="buttosn" data-server="1">
<i class="fa fa-play-circle"></i>
<?php if($field[\'name\'] != \'\') echo esc_attr( $field[\'name\'] ); ?>
</a>
</li>
<?php } ?>
<?php endif; ?>
</ul>
<div class="tabs-cntn">
<div id="services-cntn-ajax">
<?php $repeatable_fields = get_post_meta($post->ID,\'repeatable_fields\', true);
if($repeatable_fields){?>
<iframe frameborder="0" src="<?php echo esc_attr( $field[\'url\'] ); ?>" allowfullscreen=""></iframe>
<?php
} else{
}
?>
<script>
var embedMe = function(id, jQobj) {
var iframes = {};
<?php $repeatable_fields = get_post_meta($post->ID, \'repeatable_fields\', true); if ( $repeatable_fields ) : ?>
<?php foreach ( $repeatable_fields as $field ) { ?>
iframes[1] = \'<iframe width="100%" height="400" src="<?php echo esc_attr( $field[\'url\'] ); ?>" frameborder="0" allowfullscreen></iframe>\';
<?php } ?>
<?php endif; ?>
if (typeof iframes[id] === \'undefined\') {
return;
}
jQobj.removeClass(\'notactive\');
jQobj.addClass(\'active\');
jQuery(\'#services-cntn-ajax\').html(iframes[id]);
}
jQuery(\'[data-server]\').click(function() {
if (jQuery(\'li\').hasClass(\'active\')) {
return;
}
jQuery(\'li\').parent().find(\'.buttosn\').removeClass(\'active\');
jQuery(\'li\').parent().find(\'.buttosn\').addClass(\'notactive\');
jQuery(this).addClass(\'active\');
jQuery(this).removeClass(\'notactive\');
var id = jQuery(this).data(\'server\');
embedMe(id, jQuery(this));
});
</script>
</div>
</div>
</div>
我的问题是它是这样显示的
<ul class="tabs-ul nav-justified clearfix">
<li>
<a href="javascript:void(0)" class="buttosn" data-server="1">
<i class="fa fa-play-circle"></i>
serve name example</a>
</li>
<li>
<a href="javascript:void(0)" class="buttosn" data-server="1">
<i class="fa fa-play-circle"></i>
serve name example</a>
</li>
<li>
<a href="javascript:void(0)" class="buttosn" data-server="1">
<i class="fa fa-play-circle"></i>
serve name example</a>
</li>
<li>
<a href="javascript:void(0)" class="buttosn" data-server="1">
<i class="fa fa-play-circle"></i>
serve name example</a>
</li>
</ul>
他们都有
data-server="1"
在这里他们有iframes[1]
<script>
var embedMe = function(id, jQobj) {
var iframes = {};
iframes[1] = \'<iframe width="100%" height="400" src="https://example.com/" frameborder="0" allowfullscreen></iframe>\';
iframes[1] = \'<iframe width="100%" height="400" src="https://example.com/" frameborder="0" allowfullscreen></iframe>\';
iframes[1] = \'<iframe width="100%" height="400" src="https://example.com/" frameborder="0" allowfullscreen></iframe>\';
iframes[1] = \'<iframe width="100%" height="400" src="https://example.com/" frameborder="0" allowfullscreen></iframe>\';
if (typeof iframes[id] === \'undefined\') {
return;
}
jQobj.removeClass(\'notactive\');
jQobj.addClass(\'active\');
jQuery(\'#services-cntn-ajax\').html(iframes[id]);
}
jQuery(\'[data-server]\').click(function() {
if (jQuery(\'li\').hasClass(\'active\')) {
return;
}
jQuery(\'li\').parent().find(\'.buttosn\').removeClass(\'active\');
jQuery(\'li\').parent().find(\'.buttosn\').addClass(\'notactive\');
jQuery(this).addClass(\'active\');
jQuery(this).removeClass(\'notactive\');
var id = jQuery(this).data(\'server\');
embedMe(id, jQuery(this));
});
</script>
我希望是这样
data-server="1"
data-server="2"
data-server="3"
data-server="4"
等
iframes[1]
iframes[2]
iframes[3]
iframes[4]
等
所以当我点击任何一台服务器时,它会显示这样的一个iFramedata-server="1"
打开iframes[1]
data-server="2"
打开iframes[2]
data-server="3"
打开iframes[3]
data-server="4"
打开iframes[4]
等
最合适的回答,由SO网友:disinfor 整理而成
您需要一个计数的迭代器。我只包括foreach
循环:
<?php $data_i = 1; // set the iterator ?>
<?php foreach ( $repeatable_fields as $field ) { ?>
<li>
<a href="javascript:void(0)" class="buttosn" data-server="<?php echo $data_i; ?>"><!-- <-- put the iterator here -->
<i class="fa fa-play-circle"></i>
<?php if($field[\'name\'] != \'\') echo esc_attr( $field[\'name\'] ); ?>
</a>
</li>
<?php $data_i++; //this is your iterator to count up. } ?>
此处相同:
<?php $iframe_i = 1; ?>
<?php foreach ( $repeatable_fields as $field ) { ?>
iframes[<?php echo $iframe_i; ?>] = \'<iframe width="100%" height="400" src="<?php echo esc_attr( $field[\'url\'] ); ?>" frameborder="0" allowfullscreen></iframe>\';
<?php $iframe_i++; } ?>