简体   繁体   English

cakephp-js文件中图像的路径

[英]cakephp - path for image inside js file

Trying to implement this jquery plugin http://www.myjqueryplugins.com/jquery-plugin/jrating i have trouble setting paths for stars in js file. 尝试实现此jquery插件http://www.myjqueryplugins.com/jquery-plugin/jrating我在设置js文件中星星的路径时遇到麻烦。 its not possible to use php inside js file right? 它不可能在js文件中使用php吗? Cause I need to set those stars path to be inside webroot folder and i don't know how to do that without WWW_ROOT constant 因为我需要将那些星星路径设置为webroot文件夹内,如果没有WWW_ROOT常量,我不知道该怎么做

(function($) {
    $.fn.jRating = function(op) {
        var defaults = {
            bigStarsPath : 'icons/stars.png'
            ...

Just make it an absolute URL instead of a relative URL: 只需将其设置为绝对URL而不是相对URL:

(function($) {
$.fn.jRating = function(op) {
    var defaults = {
        bigStarsPath : '/icons/stars.png'  //<-- notice the "/" before "icons"
        ...

That tells it to look for an 'icons' folder within the 'webroot' folder. 这告诉它在“ webroot”文件夹中查找“ icons”文件夹。

The same goes for any other files in the webroot. webroot中的任何其他文件也是如此。 Sometimes you want to include css or javascript file that's within a library or something - in that case, you can include it by setting a "/" first - like this: /bootstrap/js/main.js . 有时您想包含库或其他内容中的css或javascript文件-在这种情况下,您可以通过先设置“ /”来包含它,例如: /bootstrap/js/main.js

There are many solutions: 有很多解决方案:

  1. Copy this code to view file (.ctp) and use: 复制此代码以查看文件(.ctp)并使用:

     bigStarsPath: '<?php echo $path_to_file; ?>' 
  2. If You can add .js files to preprocessor of PHP: Parse a JavaScript file through PHP 如果可以将.js文件添加到PHP的预处理器中,请执行以下操作: 通过PHP解析JavaScript文件

  3. Set in view js variable and read in js. 在view js变量中设置并读入js。

in your header file put this code below script with set constant variable and use that JavaScript variable name called bipartisanship in your JS file, JS must be include after this code. 在您的头文件中,将此代码放在带有设置常量变量的脚本下面,并在JS文件中使用称为bipartisanship的JavaScript变量名称,此代码后必须包含JS。

&lt;?php define('bipartisanship','/icons'); ?&gt;
    &lt;script type="text/javascript"&gt;
        var bipartisanship = '&lt;?php echo bipartisanship; ?&gt;'
    &lt;/script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM