如何为图像链接设置ContextPath [英] How to set ContextPath for an image link

查看:77
本文介绍了如何为图像链接设置ContextPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在HTML中放置图片的上下文路径。< img src =/ MyWeb / images / pageSetup.gif>



这里/ MyWeb是硬编码的ContextPath。



我正在使用< img src = contextPath +/ images / pageSetup.gif> 但图像不显示。是否有任何选项。

解决方案

不能在 src 属性。要做你想做的事,试一下这样的代码:

  var img = new Image(); 
img.src = contextPath +/images/pageSetup.gif;
document.getElementById('display')。appendChild(img);

这里的目标;您想要显示图片的地方,是一个div或span,并带有id显示。



Demo



使用HTML,您必须采取一些额外的流量来产生错误,以便您可以替换图像,或者你可以发送一些流量谷歌的方式。

 < img src ='notAnImage'onerror ='this。 src = contextPath +/images/pageSetup.gif'> b 


$ b

>演示



不要使用此功能。


i am trying to put context path for an image in HTML.<img src="/MyWeb/images/pageSetup.gif">

Here /MyWeb is the ContextPath which is hardcoded. How can i get dynamically.

i am using as <img src=contextPath+"/images/pageSetup.gif">but image is not displaying. Is there any option.

解决方案

You can't write JavaScript in the src attribute. To do what you want, try some code like this:

var img = new Image();
img.src = contextPath + "/images/pageSetup.gif";
document.getElementById('display').appendChild(img);

Here the target; the place where you want to display the image, is a div or span, with the id display.

Demo

With HTML, you'll have to take some extra traffic of producing an error, so you can replace the image, or you can send some traffic Google's way. Please do not use this:

<img src='notAnImage' onerror='this.src= contextPath + "/images/pageSetup.gif" '>

Demo

Do not use this.

这篇关于如何为图像链接设置ContextPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆