如何写一张正确的图片路径

How to write a correct path to picture?

本文关键字:路径 何写一 张正确      更新时间:2023-09-26

我有一个文件(jquerymenucss.js)在app/assets/javascripts。我在app/assets/images中有一张图片(箭头向下。gif)。但是这张照片没有在页面上显示!!(查看空图片)。

如何正确写路径?

My js file:

  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中分配图像路径,例如img元素的src属性,则路径将相对于文档(而不是您正在运行它的脚本)。路径是相对于客户端看到的文档URL

(这与CSS不同,在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)文件中,而不使用额外的CSS文件,你应该使用绝对url。因为你可能会包括你的插件在所有形式的页面(foo/, bar/this, there/is/only/zuul)。比较T.J.克劳德的回答。从CSS文件中可以使用相对url,相对于CSS文件。