如何写一张正确的图片路径
How to write a correct path to picture?
我有一个文件(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文件。
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- Canvas+svg路径动画,在路径中的特定点暂停一段时间,然后继续
- 有没有一条清晰的路径,我可以实现一个在 ScalaJS + React 中广泛使用上下文的 React 组件
- 我如何可以忽略以前的选择,只写最后一次选择
- 向下滑动菜单-如何一次只允许打开一个路径
- Javascript 字符串验证.如何在字符串中只写一次字符,并且只在开头写一次
- 避免一遍又一遍地写同一个单词
- 只写一次“return false”来处理点击许多链接
- 扭曲拉斐尔的一条“路径”
- 如何检测在 javascript 中单击了 svg 路径的哪一段
- 使用 redux-simple-router 导航到另一条路径
- JavaScript中有没有一种方法可以在解析HTML时在浏览器加载图像之前更改图像路径
- 如何打印html代码,这样我只写过一次
- 有没有更好的方法来写这篇文章,或者用一种更短更紧凑的方法
- Snap.svg沿着一条路径拖动一个组并保存位置
- RaphaelJS:在另一个SVG元素上绘制一条路径
- 我怎样才能在D3中的矩形上写一段文字呢
- 让一条路径指向另一条路径的中间
- 如何写一张正确的图片路径
- 路径应该是一条直线