假设我的域名是www.example.com,我有一个类别,其中包含很多项目。例如,旅行、书籍、教育。现在,如果我单击“旅行”,则获取请求为 www.example.com/category/travel-areas
。现在,我想在特别是从类别中单击“旅行”时执行脚本,例如 <script>alert("hi")</script>
当我打开www.example.com/category/travel-areas
时它将被执行
这是我的category.php 文件:-
<div class="content">
<?php tie_breadcrumbs() ?>
<?php
$category_id = get_query_var('cat') ;
$tie_cats_options = get_option( 'tie_cats_options' );
if( !empty( $tie_cats_options[ $category_id ] ) )
$cat_options = $tie_cats_options[ $category_id ];
?>
<div class="page-head">
<h1 class="page-title">
<?php echo single_cat_title( '', false ) ?>
</h1>
<?php if( tie_get_option( 'category_rss' ) ): ?>
<a class="rss-cat-icon ttip" title="<?php _eti( 'Feed Subscription' ); ?>" href="<?php echo get_category_feed_link($category_id) ?>"><i class="fa fa-rss"></i></a>
<?php endif; ?>
<div class="stripe-line"></div>
<?php
if( tie_get_option( 'category_desc' ) ):
$category_description = category_description();
if ( ! empty( $category_description ) )
echo '<div class="clear"></div><div class="archive-meta">' . $category_description . '</div>';
endif;
?>
</div>
<?php get_template_part( 'framework/parts/slider-category' ); ?>
<?php $loop_layout = ( !empty( $cat_options[ 'category_layout' ] ) ? $cat_options[ 'category_layout' ] : tie_get_option( 'category_layout' ) ); ?>
<?php get_template_part( 'loop' ); ?>
<?php if ($wp_query->max_num_pages > 1) tie_pagenavi(); ?>
</div> <!-- .content -->
最佳答案
在您的代码中没有显示确切链接的部分。但我可以建议你有不同区域的链接列表。例如:
<a href="#" class="travel-area" data-id="1"> Area title</a>
所以我在Jsfiddle中添加了一些示例。正如你所看到的,JS代码的某些部分只有在传递if表达式时才会被执行。您可以更改该表达式,因此添加一些额外的检查。
关于javascript - 为 php 中的特定 get 请求执行 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35557546/