我使用下面的函数来判断图片的长和宽,如果宽度超过700px则缩放到700px
function DownImage(imgd){
var image = new Image();
image.src = imgd.src;
if(image.width>0 && image.height>0){
if(image.width>=700){
imgd.width=700;
imgd.height=(image.height*700)/image.width;
var link = document.createElement('a');
link.href = imgd.src;
link.innerHTML = imgd;
}else{
imgd.width=image.width;
imgd.height=image.height;
}
}
}现在需要在此基础上加一个条件:当宽度大约700px缩放的同时给该图片加一个链接,地址就是当前图片的src.同时在该图片的下方用文字显示出来这张图片的长和宽
function DownImage(imgd){
var image = new Image();
image.src = imgd.src;
if(image.width>0 && image.height>0){
if(image.width>=700){
imgd.width=700;
imgd.height=(image.height*700)/image.width;
var link = document.createElement('a');
link.href = imgd.src;
link.innerHTML = imgd;
}else{
imgd.width=image.width;
imgd.height=image.height;
}
}
}现在需要在此基础上加一个条件:当宽度大约700px缩放的同时给该图片加一个链接,地址就是当前图片的src.同时在该图片的下方用文字显示出来这张图片的长和宽
function imageResize(img, width, height) {
var image = new Image();
var iwidth = width; //定义允许图片宽度
var iheight = height; //定义允许图片高度
image.src = img.src;
if (image.width > 0 && image.height > 0) {
if (image.width / image.height >= iwidth / iheight) {
if (image.width > iwidth) {
img.width = iwidth;
img.height = (image.height * iwidth) / image.width;
}
else {
img.width = image.width;
img.height = image.height;
}
}
else {
if (image.height > iheight) {
img.height = iheight;
img.width = (image.width * iheight) / image.height;
}
else {
img.width = image.width;
img.height = image.height;
}
}
}
}
var b=a.outerHTML
b='<a href="http://www.163.com">'+b+'</a>'
a.outerHTML=b;
其他浏览器没outerHTML属性
你可以自己用js拼凑html
反正就是外边加一个a标签
另外用onclick事件也不错
onclick只能用open方式打开窗口容易被浏览器拦截而a可以本窗口打开也可以新窗口打开且不容易被浏览器拦截
所以感觉还是a比较好
偶在7楼的方法你试了么
function DownImage(imgd) {
var image = new Image();
image.src = imgd.src;
if (image.width > 0 && image.height > 0) {
if (image.width >= 300) {
image.width = 100;
image.height = 100;
var link = document.createElement('a');
link.href = imgd.src;
link.appendChild(image);
document.body.appendChild(link);
}else{
document.body.appendChild(image);
}
}
}
DownImage({src:"http://topic.csdn.net/u/ui/styles/default/topic/bgUserCard.gif"});