duanlv2788
2014-04-01 21:47
浏览 51

在jQuery中使用php添加div类

My variable contains a color name. My example:

<?php
$myvar = blueColour;
?>

I need to add this value to body of a html page using jQuery:

<script type="text/javascript">
jQuery(body).addClass("<?php echo $myvar; ?>");
</script>

Can I use php inside jQuery this way? Thank you.

EDIT: its an wordpress site. I use this in a if to add that class on a specific page template.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • drfcaw7460 2014-04-01 21:49
    已采纳
    <script type="text/javascript">
    jQuery("body").css("color", "<?php echo $myvar; ?>");
    </script>
    
    打赏 评论
  • dongyi5070 2014-04-01 21:49

    yes, as long as your html file is interpreted by php (having .php/.phtml extension)

    打赏 评论
  • dongque8332 2014-04-01 21:57

    I did it. Worked. Correct is:

    jQuery('body').addClass("<?php echo $myvar; ?>");
    

    with

    jQuery('body')
    

    not

    jQuery(body)
    
    打赏 评论
  • dongxian7194 2014-04-01 21:57
    if ( is_page_template('nameOfYouTemplate.php') ) {
    
        echo '<body class='. $myvar .'>';
    
     } else {
        echo '<body>';
     }
    
    打赏 评论

相关推荐 更多相似问题