如何从页眉到正文调用JavaScript?
我怎么能从页眉把它称为身体,我在互联网上尝试了所有的方法,但它根本不起作用,我是否遗漏了一些明显的东西?你会怎么做如何从页眉到正文调用JavaScript?,javascript,jquery,html,wordpress,Javascript,Jquery,Html,Wordpress,我怎么能从页眉把它称为身体,我在互联网上尝试了所有的方法,但它根本不起作用,我是否遗漏了一些明显的东西?你会怎么做 <script type="text/javascript" src="/wp-content/themes/dw-minion/assets/css/jstick/jquery.js"></script> <script type="text/javascript" src="/wp-content/themes/dw-minion/assets/c
<script type="text/javascript" src="/wp-content/themes/dw-minion/assets/css/jstick/jquery.js"></script>
<script type="text/javascript" src="/wp-content/themes/dw-minion/assets/css/jstick/jquery.stickem.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.container').stickem();
});
</script>
jQuery(文档).ready(函数($){
$('.container').stickem();
});
我应该补充一点,在WordPress上应用content.php中的JavaScript时,我运行了多个实例。这就是问题所在吗
以下是我的content.php文件内容:
<div class="title-wrapper">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<a href="<?php the_permalink(); ?>#comments" title="<?php comments_number( 'No Comments', '1 Comment', '% Comments' ); ?>">
<div class="commentnumber"><?php comments_number( '0', '1', '%' ); ?></div>
</a>
</div>
<div class="container">
<div class="stickem-container">
<div class="thelinks stickem">
<div class="sharelinks">
<div class="sharepinterest">
<?php echo get_simple_local_avatar( $id_or_email, $size, $default, $alt ); ?>
</div>
<a href="http://www.facebook.com/sharer/sharer.php?s=100&p[url]=<?php the_permalink(); ?>&p[images][0]=http://www.otlcampaign.org/sites/default/files/journey-for-justice-mlk-memorial.jpg&p[title]=<?php the_title(); ?>&p[summary]=Click+to+enlarge">
<div class="sharefacebook"></div>
</a>
<a href="http://twitter.com/home?status=<?php the_title(); ?>+<?php the_permalink(); ?>">
<div class="sharetwitter"></div>
</a>
<div class="sharegoogle"></div>
</div>
</div>
<div class="post-wrapper">
<div class="entry-content">
<a href="<?php the_permalink(); ?>"><?php the_content(); ?></a>
</div>
</div>
</div>
</div><a>
脚本应该与
functions.php
一起排队,而不是直接在其他主题模板文件中排队。此外,主题似乎捆绑了jQuery,这是做错了()™.
任何一个都可以用来过滤不同页面中的排队
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_so_18774457' ) );
function enqueue_so_18774457()
{
if( is_single() )
{
wp_enqueue_script(
'stickem-js',
get_stylesheet_directory_uri() . '/assets/css/jstick/jquery.stickem.js',
array( 'jquery' ) // This enqueues jQuery as a dependency
);
}
}
对于小型脚本,如$('.container').stickem()代码>,可用于:
add_action( 'wp_footer', 'footer_so_18774457' );
function footer_so_18774457()
{
if( !is_single() )
return;
echo "
<script type='text/javascript'>
jQuery(document).ready(function($) { $('.container').stickem(); });
</script>";
}
add_action('wp_footer','footer_so_18774457');
函数页脚_so_18774457()
{
如果(!is_single())
返回;
回声“
jQuery(document).ready(函数($){$('.container').stickem();});
";
}
在那段代码之前,您是否也加入了jQuery?“我在互联网上尝试了所有方法”显然没有。假设您包含jQuery并且它存储在jQuery变量中,并且.container存在并且.stickem()可以工作,我们无法确认/否认您提供的代码。作为旁注,您应该使用enque_script
方法在Wordpress中添加脚本并设置依赖项,而不是在脚本标记中硬编码。首先,找出它不起作用的原因。是否触发就绪事件中的代码?是否找到.container
元素?如果两者都是肯定的,那么问题在于插件。如果两者都不是,那么您的控制台中可能存在javascript错误。如果一个是“是”,另一个是“否”,那么请找出.container
不存在的原因。您试图选择的元素是什么样子的?