最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - On-click Box shadow Shadow - Javascript (not jquery) - Stack Overflow

programmeradmin3浏览0评论

I want a drop shadow to appear upon click of a image. I don't use Jquery at the moment so if possible provide a java-script solution. Heres my fiddle: /

I would also like for the box shadow to only be active on whatever item is Currently Selected. (the last item clicked should be the only one with the shadow)

    window.onload = function(){
var box1 = document.getElementById('thumbs');
var box1Div1 = box1.getElementsByTagName('input');
var img1 = box1Div1[0]
    console.log(img1);

    img1.onclick = function(){
    img1.setAttribute("class", "BoxShadow");
   };
   };

I want a drop shadow to appear upon click of a image. I don't use Jquery at the moment so if possible provide a java-script solution. Heres my fiddle: http://jsfiddle/zUNhD/7/

I would also like for the box shadow to only be active on whatever item is Currently Selected. (the last item clicked should be the only one with the shadow)

    window.onload = function(){
var box1 = document.getElementById('thumbs');
var box1Div1 = box1.getElementsByTagName('input');
var img1 = box1Div1[0]
    console.log(img1);

    img1.onclick = function(){
    img1.setAttribute("class", "BoxShadow");
   };
   };
Share Improve this question asked Nov 5, 2012 at 8:46 ModSModS 8463 gold badges16 silver badges31 bronze badges 2
  • 1 Like this: jsfiddle/zUNhD/20 ? – Passerby Commented Nov 5, 2012 at 9:12
  • @Passerby Yes Thank you very much! If you want repost this as a "answer" and i will accept. much appreciated. – ModS Commented Nov 5, 2012 at 9:27
Add a ment  | 

3 Answers 3

Reset to default 2

Extending from ment:

You can use :active selector to response to "click" event, and :focus selector to response to, well, "focus" event:

input:active, input:focus{
    box-shadow:2px 2px 2px blue;
}

fiddle

The function hasn't been called in your code.

window.onload = function(){
var box1 = document.getElementById('thumbs');
var box1Div1 = box1.getElementsByTagName('input');
var img1 = box1Div1[0];      
img1.onclick = function(){
img1.setAttribute("class", "DropShad");
};
}();

Here is the working fiddle ... http://jsfiddle/zUNhD/18/

See this jsfiddle for some simplifications of your code. The css class wasn't assigned right, it should be preceded by a dot. Put your script tag near the ending </body> tag and use:

var box1 = document.getElementById('thumbs');
var box1Div1 = box1.getElementsByTagName('input');
var img1 = box1Div1[0]

img1.onclick = function(){
  img1.className = "DropShad";
};
发布评论

评论列表(0)

  1. 暂无评论