jQuery 变量未定义 php include


jQuery variable undefined with php include

我正在尝试在jQuery中使用一个变量,它包含在php中,页脚中 IMG 的 attr rel

我尝试使用它来更改页脚中同一 img 中的 attr src。

如果我在没有包含的情况下使用它,那么它可以工作。但是对于包含它没有。

我总是得到../img/undefined.svg

我怎样才能让它成为../img/facebook.svg 或 ../img/twitter.svg ?

在我的 php 文件中,我使用:

<?php include '../include/header.php';?>
<?php include '../include/footer.php';?>

在标头中.php是javascript,jQuery和html:

    <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" type="text/css" href="../css/stijl.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $("footer img").hover(
            function(){
                var src = $(this).attr('rel');
            $(this).attr('src', '../img/' + src + '-n.svg');
                }
        ,
        function(){
            var src = $(this).attr("rel");
            $(this).attr('src', '../img/' + src + '.svg');
        }
        );
    });
</script>

在页脚中.php只是html:

    <footer>
    <div class="content">
        <a rel="twitter" href="#">
            <img src="../img/twitter.svg">
        </a>
        <a rel="facebook" href="#">
            <img src="../img/facebook.svg">
        </a>
    </div>
</footer>

$(this)引用没有rel属性的img标签。 但是,它的父标记确实如此。 尝试:

var src = $(this).parent().attr('rel');

或者,如果标记可能略有变化,这可能更可靠:

var src = $(this).closest('a').attr('rel');