本文目錄一覽:
- 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 循環遍曆數組。
html
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
/html
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″
titleapple 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();
}
}
在對應地方調用即可
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/279624.html