doolo00026 2019-05-11 20:31 采纳率: 0%
浏览 80
已采纳

无法在JS中添加PHP [重复]

This question already has an answer here:

I am trying to add a PHP code to echo a text inside of JavaScript but when I try the code it's don't echo the text it echoes 0

<script>
window.onload = function(){
    <?php if (empty($dark_mode)){ echo '$('*').toggleClass("light") document.getElementById("*").classList.remove("dark");';}else{ echo '$('*').toggleClass("light")  document.getElementById("*").classList.remove("light");';} ?>
};
</script>
</div>
  • 写回答

3条回答 默认 最新

  • dpjo15650 2019-05-11 21:05
    关注

    i had the same issue on past i did use php to print all javascript with php vars too , in your case it well be :

         <?php
         echo "<script>";
         echo "window.onload = function(){";
         if (empty($dark_mode)){
                  echo "$('*').toggleClass('light') document.getElementById('*').classList.remove('dark');";
         }else{ 
         echo "$('*').toggleClass('light') document.getElementById('*').classList.remove('light');";
         }
    

    echo ""; ?>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?