与JavaScript相关的语法&;PHP

与JavaScript相关的语法&;PHP,javascript,php,Javascript,Php,PHP转换为JavaScript变量。我尝试了div方法,为此我使用了以下代码: <div class="service-container" data-service=" <?php echo bp_loggedin_user_domain() . BP_XPROFILE_SLUG . '/change-avatar/'; ?>" style="display:none;"><\div> <?php $avatarcount = 0; i

PHP转换为JavaScript变量。我尝试了div方法,为此我使用了以下代码:

    <div class="service-container" data-service="
<?php echo bp_loggedin_user_domain() . BP_XPROFILE_SLUG . '/change-avatar/'; ?>"
 style="display:none;"><\div>

<?php 
$avatarcount = 0;
if ( is_user_logged_in() ) {
global $bp;
global $current_user;
get_currentuserinfo();
$local = BP_AVATAR_UPLOAD_PATH . '/avatars/' . $bp->loggedin_user->id;
$useremail = $current_user->user_email;

if (!file_exists($local) ) {
echo '<script type="text/javascript">
$(document).ready(function() {
    $('.service-container').each(function() {
        var container = $(this);
        var service = container.data('service');
        if(document.URL.search("change-avatar") != -1) { } else  {
               window.location = service;}
    });
});
      </script>';
}}?>

有什么我想不出来的?

你可以随心所欲地做,这样你就不必使用escape等

<?php
if (!file_exists($local) ) {
?>
<script type="text/javascript">
$(document).ready(function() {
    $('.service-container').each(function() {
        var container = $(this);
        var service = container.data('service');
        if (document.URL.search("change-avatar") != -1) {} else {
            window.location = service;
        }
    });
});
</script>;
<?php
}
?>

$(文档).ready(函数(){
$('.service container')。每个(函数(){
var容器=$(此);
var service=container.data('service');
if(document.URL.search(“change avatar”)!=-1{}else{
window.location=服务;
}
});
});
;

您应该像这样将内部回显“”更改为“”

 $('.service-container'). ==========  $(".service-container").

转义您的引号-
$(\'.service container\')
container.data(\'service\')或使用
…代码的颜色应该给你一个线索。单引号字符串中的单引号不起作用nicley of Course修复了语法,但服务值似乎为零,因为没有重定向发生您的代码,服务值似乎有问题,因为没有重定向发生。基本上这是您的代码,而不是我的代码;)
 $('.service-container'). ==========  $(".service-container").