js开关语句代码案例,javascript的开关灯代码

发布时间:2022-11-24

本文目录一览:

  1. 求自动点击按钮的代码,最好是Javascript
  2. Js 中for in 的用法,以及案例
  3. javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=d
  4. 怎样用js编写一个开关按钮实现动态效果
  5. 用JS IF语句中加入关闭网页代码

求自动点击按钮的代码,最好是Javascript

如果用 jQuery,很简单:

$("#123").trigger('click');

如果只用原生的 JavaScript,先做一个函数:

function fireEvent(element, event) {
  if (document.createEventObject) {
    // dispatch for IE
    var evt = document.createEventObject();
    return element.fireEvent('on' + event, evt);
  } else {
    // dispatch for firefox + others
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent(event, true, true); // event type, bubbling, cancelable
    return !element.dispatchEvent(evt);
  }
}

然后:

fireEvent(document.getElementById('123'), 'click');

补充:

回答你的问题如下 function 就插在我的 if 语句之前应该没问题吧? 没问题,但是最好不要写在循环中。 对那个按钮的代码插入有要求吗? 有要求。你原样抄的话,得保证 id="123",否则跟着改。 最后那句得稍微改一下,漏了个 document.

fireEvent(document.getElementById('123'), 'click');

附上测试代码

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>测试</title>
  <style></style>
  <script type="text/javascript">
    function fireEvent(element, event) {
      if (document.createEventObject) {
        // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on' + event, evt);
      } else {
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true); // event type, bubbling, cancelable
        return !element.dispatchEvent(evt);
      }
    }
    fireEvent(document.getElementById('123'), 'click');
  </script>
</head>
<html>
  <body>
    ABCDE
    <input type="button" name="123" id="123" value="按钮" onclick="alert('OK');">
  </body>
</html>

Js 中for in 的用法,以及案例

for...in 语句用于对数组或者对象的属性进行循环操作。 for ... in 循环中的代码每执行一次,就会对数组的元素或者对象的属性进行一次操作。

语法:

for (变量 in 对象) {
  在此执行代码
}

“变量”用来指定变量,指定的变量可以是数组元素,也可以是对象的属性。

实例:

使用 for ... in 循环遍历数组。

<body>
  <script type="text/javascript">
    var x;
    var mycars = new Array();
    mycars[0] = "Saab";
    mycars[1] = "Volvo";
    mycars[2] = "BMW";
    for (x in mycars) {
      document.write(mycars[x] + "<br />");
    }
  </script>
</body>

javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=d

这段程序需要写在一行上才能执行,功能是把网页的所有图片全部飞舞起来,下面分行写为程序的格式,然后添加简单的注释进行说明: javascript: 开头的东西出现在浏览器的地址的开始,表示后面的是 JavaScript 程序语句代码,不是文件名。

// 第一行初始化系列代码
R = 0;
x1 = .1;
y1 = .05;
x2 = .25;
y2 = .24;
x3 = 1.6;
y3 = .24;
x4 = 300;
y4 = 200;
x5 = 300;
y5 = 200;
// 下面的语句执行后 DI 是一个数组,数组的元素是页面上的所有图片
DI = document.images;
// DIL 为数组中的元素个数,也就是是网页上图片的格式
DIL = DI.length;
// 定义一个函数,修改每个图片的位置
function A() {
  for (i = 0; i < DIL; i++) {
    DIS = DI[i].style;
    DIS.position = 'absolute';
    DIS.left = Math.sin(R * x1 + i * x2 + x3) * x4 + x5;
    DIS.top = Math.cos(R * y1 + i * y2 + y3) * y4 + y5;
  }
  R++;
}
// 下面的语句设置每 5 毫秒执行一下函数
setInterval('A()', 5);
// 最后以下面的语句结束
void(0);

怎样用js编写一个开关按钮实现动态效果

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>apple button</title>
  </head>
  <body>
    <div id="div1">
      <div id="div2"></div>
    </div>
  </body>
</html>

CSS

#div1 {
  width: 170px;
  height: 100px;
  border-radius: 50px;
  position: relative;
}
#div2 {
  width: 96px;
  height: 96px;
  border-radius: 48px;
  position: absolute;
  background: white;
  box-shadow: 0px 2px 4px rgba(0,0,0,0.4);
}
.open1 {
  background: rgba(0,184,0,0.8);
}
.open2 {
  top: 2px;
  right: 1px;
}
.close1 {
  background: rgba(255,255,255,0.4);
  border: 3px solid rgba(0,0,0,0.15);
  border-left: transparent;
}
.close2 {
  left: 0px;
  top: 0px;
  border: 2px solid rgba(0,0,0,0.1);
}

JavaScript

window.onload = function() {
  var div2 = document.getElementById("div2");
  var div1 = document.getElementById("div1");
  div2.onclick = function() {
    div1.className = (div1.className == "close1") ? "open1" : "close1";
    div2.className = (div2.className == "close2") ? "open2" : "close2";
  }
}

用JS IF语句中加入关闭网页代码

function closeWebPage() {
  if (navigator.userAgent.indexOf("MSIE") > 0) { // close IE
    if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
      window.opener = null;
      window.close();
    } else {
      window.open('', '_top');
      window.top.close();
    }
  } else if (navigator.userAgent.indexOf("Firefox") > 0) { // close firefox
    window.location.href = 'about:blank';
  } else { // close chrome; It is effective when it is only one.
    window.opener = null;
    window.open('', '_self');
    window.close();
  }
}

在对应地方调用即可