【问题标题】:How to write a correct path to picture?如何写出正确的图片路径?
【发布时间】:2014-07-07 20:53:19
【问题描述】:

我在 app/assets/javascripts 中有一个文件 (jquerymenucss.js)。我在 app/assets/images 中有一张图片 (arrow-down.gif)。但是这张照片在页面上看不到!!! (查看空白图片)。

如何正确写入路径?

我的js文件:

  var arrowimages={down:['downarrowclass', 'arrow-down.gif', 25], right:['rightarrowclass', 'arrow-right.gif']}

var jquerycssmenu={

fadesettings: {overduration: 350, outduration: 100}, //duration of fade in/ out animation, in milliseconds

buildmenu:function(menuid, arrowsvar){
    jQuery(document).ready(function($){
        var $mainmenu=$("#"+menuid+">ul")
        var $headers=$mainmenu.find("ul").parent()
        $headers.each(function(i){
            var $curobj=$(this)
            var $subul=$(this).find('ul:eq(0)')
            this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
            this.istopheader=$curobj.parents("ul").length==1? true : false
            $subul.css({top:this.istopheader? this._dimensions.h+"px" : 0})
            $curobj.children("a:eq(0)").css(this.istopheader? {paddingRight: arrowsvar.down[2]} : {}).append(
                '<img src="'+ (this.istopheader? arrowsvar.down[1] : arrowsvar.right[1])
                +'" class="' + (this.istopheader? arrowsvar.down[0] : arrowsvar.right[0])
                + '" style="border:0;" />'
            )
            $curobj.hover(
                function(e){
                    var $targetul=$(this).children("ul:eq(0)")
                    this._offsets={left:$(this).offset().left, top:$(this).offset().top}
                    var menuleft=this.istopheader? 0 : this._dimensions.w
                    menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
                    $targetul.css({left:menuleft+"px"}).fadeIn(jquerycssmenu.fadesettings.overduration)
                },
                function(e){
                    $(this).children("ul:eq(0)").fadeOut(jquerycssmenu.fadesettings.outduration)
                }
            ) //end hover
        }) //end $headers.each()
        $mainmenu.find("ul").css({display:'none', visibility:'visible'})
    }) //end document.ready
}
}

//build menu with ID="myjquerymenu" on page:
jquerycssmenu.buildmenu("myjquerymenu", arrowimages)

css 文件

.jquerycssmenu {
    font: bold 12px Verdana;
    padding-left: 30px; /*offset of tabs relative to browser left edge*/
}

.jquerycssmenu ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
}

    /*Top level list items*/
.jquerycssmenu ul li {
    position: relative;
    display: inline;
    float: left;
}

    /*Top level menu link items style*/
.jquerycssmenu ul li a {
    display: block;
    padding: 5px 20px 10px 0;
    margin-right: 3px; /*spacing between tabs*/
    color: #aa9685;
    font-size: 12px;
    font-weight: bold;
    text-decoration: none;
}

.jquerycssmenu ul li a:hover {
    color: #f00;
}

    /*1st sub level menu*/
.jquerycssmenu ul li ul {
    position: absolute;
    left: 0;
    display: block;
    visibility: hidden;
    border-top: 1px solid #973133;
}

    /*Sub level menu list items (undo style from Top level List Items)*/
.jquerycssmenu ul li ul li {
    display: list-item;
    float: none;
}

    /*All subsequent sub menu levels vertical offset after 1st level sub menu */
.jquerycssmenu ul li ul li ul {
    top: 0;
}

    /* Sub level menu links style */
.jquerycssmenu ul li ul li a {
    font: normal 13px Verdana;
    width: 160px; /*width of sub menus*/
    background: #761f20;
    color: #ffffff;
    padding: 8px 5px;
    border-top-width: 0;
    font-size: 11px;
}

.jquerycssmenu ul li ul li a:hover { /*sub menus hover style*/
    background: #b14546;
    color: black;
}

    /* ######### CSS classes applied to down and right arrow images  ######### */

.downarrowclass {
    position: absolute;
    top: 7px;
    right: 5px;

}

.rightarrowclass {
    position: absolute;
    top: 5px;
    right: 5px;
}

【问题讨论】:

  • 写入相对于显示图像的页面的路径
  • 您应该提供更多信息(例如,各个资产的位置以及它们如何相互引用)。

标签: javascript ruby-on-rails assets


【解决方案1】:

如果您在 JavaScript 中分配图像路径,例如 img 元素的 src 属性,那么路径将相对于 文档 (不是您运行它的脚本)。路径是相对于文档的 URL客户端看到的

(这与 CSS 不同,其中路径是相对于客户端看到的 CSS 文件的 URL,而不是使用 CSS 文件的文档。)

例如:

  • 如果客户端(浏览器)在http://www.example.com/foo/document.html 看到文档,并且您的代码(从任何地方)包含在该文档中,并且提供图像的实际 URL 是(猜测)http://www.example.com/assets/images/down-arrow.gif,那么你需要../assets/images/down-arrow.gif/assets/images/down-arrow.gif

  • 如果客户端在http://example.com/doc.html看到文档并且图片在http://example.com/app/assets/images/down-arrow.gif,那么相对路径是app/assets/images/down-arrow.gif(或/app/assets/images/down-arrow.gif)。

【讨论】:

  • 以及我如何在 js 中编写此字符串:var arrowimages={down:['downarrowclass', 'app/assets/images/arrow-down.gif', 25] ?
  • @Cat:这是一个字符串。您可以像编写任何其他字符串一样编写它。由于您从未说过文档的外部 URL 和图像的外部 URL 之间的关系是什么,因此无法准确地告诉您您需要什么。但是上面告诉如何弄清楚。这是一个相对 URL,相对于图像所在的文档。
【解决方案2】:

如果您一心只想将所有插件放在一个 (JS) 文件中而不使用额外的 CSS 文件,则应该使用绝对 URL。因为您可能会将您的插件包含在所有形式的页面中(foo/、bar/this、there/is/only/zuul)。比较T.J. Crowder's answer。在 CSS 文件中,您可以使用相对于 CSS 文件的相对 URL。

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多