0

<body>有没有办法在php中的标签之前将脚本注入页面?我已经看到了一些关于如何使用 jquery 执行此操作的示例,但是 jquery 会导致我的网站的某些部分无法正常运行,因此我需要一个 php 代码,以便我可以有选择地在页面的开始标记之前注入脚本。

IE。

.....
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="jquery.internads.js"></script>
<script>

    $(document).ready(function(){       

        // Call Intern Ads              
        $().tcInternAds({
            title: "Support Our Sponsors!",
            url: "adpage.html",
            timeout: 10,
            deplay: 0,
            wait: 0,
            closeable: true
        });

    });

</script>
<body>
<div class="xxxxx">
...............</div>

如果可能的话,我希望能够使用 php,因此我可以将代码包含在我需要调用脚本的模板文件中,同时避免全局包含。

有人能帮忙吗?

最好的,迈克

4

3 回答 3

0

我找到了解决问题的方法,而无需注入或替换头代码。问题是我的网站包含 mootools 并且 jquery 与之冲突。我通过使用 jQuery noConflict 解决了这个问题,它定义了一个要使用的命名空间,因此通过使用自定义变量而不是美元 $ 来解决这个问题。

这是我解决 jquery/mootools 冲突的工作解决方案。

<link href="/internAds.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="/jquery.internads.js"></script>
<script>
var $j = jQuery.noConflict();
    $j(document).ready(function(){      

        // Call Intern Ads              
        $j().tcInternAds({
            title: "Support Our Sponsors!",
            url: "/adpage.html",
            timeout: 15,
            deplay: 0,
            wait: 5,
            closeable: false
        });

    });

</script>

我感谢大家的时间,我不确定我是如何忽视冲突的。

最好的,

于 2012-05-26T21:32:52.323 回答
0

不会简单地把它放在<head>作品中吗?

<head>
.....
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="jquery.internads.js"></script>
<script>

    $(window).load()(function(){       

        // Call Intern Ads              
        $().tcInternAds({
            title: "Support Our Sponsors!",
            url: "adpage.html",
            timeout: 10,
            deplay: 0,
            wait: 0,
            closeable: true
        });

    });

</script>
</head>
<body>
<div class="xxxxx">
...............</div>
于 2012-05-26T20:28:06.213 回答
0

将您的内容放入 index.php(或其他任何名称)中。

它应该如下所示:

<?php

...phpcode...

?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="jquery.internads.js"></script>
<script>
...
</script>
<body>
<div class="xxxxx">

<?php

another php code

?>

</div>
于 2012-05-26T20:32:41.890 回答