用onclick获取Var

时间:2017-08-03 04:33:43

标签: javascript html

我想只在onclick事件上让我网站上的每张图片都更大。当然我可以为每个Pic获取Java代码,但我想要用一个命令打开所有图片!感谢

function pic(choice, click){
    p = choice;
    p.style.width = "200px";
}
<div onclick="pic('b1')" style="position: absolute; top: 100px; left: 100px; width: 100px; height: 100px; background-color: red;"></div>

<div onclick="pic('b2')" style="position: absolute; top: 100px; left: 500px; width: 100px; height: 100px; background-color: red;"></div>

3 个答案:

答案 0 :(得分:1)

您必须提供对该元素的引用才能从JS访问该元素。试试这个,

@media handheld, only screen and (max-width: 480px){table td {
display: block!important;
clear: both;
width: 100%!important;}}
function pic(choice, click){
	p = document.getElementById(choice);
  p.style.width = "200px";
}

答案 1 :(得分:0)

您可以通过在触发器中发送onClick来发送调用this事件的元素,如下所示:

HTML:

<div onclick="pic(this)" style="position: absolute; top: 100px; left: 100px; width: 100px; height: 100px; background-color: red;"></div>

JS:

function pic(element){
  element.style.width = "200px";
}

答案 2 :(得分:0)

这是一个简单的jQuery示例 https://jsfiddle.net/thebeast/0e6yLk2h/

HTML:

   <div class="box one"></div>
   <div class="box two"></div>

CSS:

   .box {
     height: 50px;
     width: 50px;
   }

   .one {
     background-color: red;
   }

   .two {
     background-color: blue;
   }

   .large {
     width: 300px;
     height: 300px;
    }

JS:

   $('.box').click(function(e) {
      var box = e.target;
      $(box).toggleClass("large");
    })