admin管理员组

文章数量:1315775

I'm totally new to JS. I'm trying to make me a bookmarklet that finds all images on a web page and adds a colorful border to them. Then, by clicking on an image I'd like to attach the image path. This is what I've got so far:

javascript:
for (var i= document.links.length; i-->0;) {
    if (document.links[i].getElementsByTagName('img').length!=0) {
        document.links[i].onclick= function() {
           window.open("=" + this.src + "");
        };
    }
}

How can I add a border to the images?

Thanks, Bob

I'm totally new to JS. I'm trying to make me a bookmarklet that finds all images on a web page and adds a colorful border to them. Then, by clicking on an image I'd like to attach the image path. This is what I've got so far:

javascript:
for (var i= document.links.length; i-->0;) {
    if (document.links[i].getElementsByTagName('img').length!=0) {
        document.links[i].onclick= function() {
           window.open("http://www.example./whatever?imgsrc=" + this.src + "");
        };
    }
}

How can I add a border to the images?

Thanks, Bob

Share Improve this question edited Mar 1, 2011 at 23:19 user142019 asked Mar 1, 2011 at 23:11 MichaelMichael 3272 gold badges6 silver badges12 bronze badges 1
  • your English isn't bad. You've only made one big mistake (than instead of then). I've seen worse. By the way, all images of a website or a web page? Those are two different things. – user142019 Commented Mar 1, 2011 at 23:18
Add a ment  | 

2 Answers 2

Reset to default 2

Try this code:

javascript:for(i=0;i<document.getElementsByTagName('img').length;i++){var imgTag=document.getElementsByTagName('img')[i];imgTag.style.border='2px solid #E8272C';imgTag.onclick=function(){return !window.open(this.src)};}void(0)

Friendly formatted view:

javascript:
for(i=0;i<document.getElementsByTagName('img').length;i++){
    var imgTag=document.getElementsByTagName('img')[i];
    imgTag.style.border='2px solid #E8272C';
    imgTag.onclick=function(){
        return !window.open(this.src);
    }
}void(0)

There is no need to call getElementsByTagName

javascript:(function(){for(var i=0;i<document.images.length;i++){var image=document.images[i];image.style.border='medium solid blue';image.onclick=function(){location.href=this.src;return false;};}})()

本文标签: javascriptAdd border to imagesStack Overflow