JS 事件、添加class Name叽唱、獲取元素樣式屈呕、設(shè)置屬性&滾動(dòng)條shi'jain

1.事件:

?<div>

? ? ? ? 我是div

? ? </div>

? ? <script>

? ? ? ? let div = document.querySelector('div');

? ? ? ? /* div是一個(gè)dom對(duì)象

? ? ? ? .onclick相當(dāng)于給對(duì)象添加了一個(gè)屬性

? ? ? ? 屬性值是一個(gè)函數(shù),點(diǎn)擊就會(huì)觸發(fā) */

? ? ? ? /* div.onclick = function (){

? ? ? ? ? ? alert('你好')

? ? ? ? } */

? ? ? ? /* 鼠標(biāo)移上的時(shí)候 */

? ? ? ? div.onmouseover = function(){

? ? ? ? ? ? div.style.background = '#ccc'

? ? ? ? }

? ? ? ? /* 鼠標(biāo)移開 */

? ? ? ? div.onmouseout = function(){

? ? ? ? ? ? div.style.background = 'none'

? ? ? ? }

? ? ? ? /* 鼠標(biāo)按鈕被按下 */

? ? ? ? div.onmousedown = function(){

? ? ? ? ? ? alert('鼠標(biāo)按下時(shí)反應(yīng)')

? ? ? ? }

? ? ? ? /* 鼠標(biāo)抬起時(shí)反應(yīng) */

? ? ? ? div.onmouseup = function(){

? ? ? ? ? ? alert('鼠標(biāo)抬起時(shí)反應(yīng)')

? ? ? ? }

? ? </script>

2.添加className:

<head>

? ? <meta charset="UTF-8">

? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">

? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">

? ? <title>Document</title>

? ? <style>

? ? ? ? .bg{

? ? ? ? ? ? width: 200px;

? ? ? ? ? ? height: 200px;

? ? ? ? ? ? background-color: beige;

? ? ? ? }


? ? </style>

</head>

<body>

? ? <div>

? ? ? ? 我是div1

? ? </div>

? ? <script>

? ? ? ? let div = document.querySelector('div');

? ? ? ? /* 添加className */

? ? ? ? div.className = 'bg';

? ? ? ? div.onclick = function(){

? ? ? ? ? ? div.className = ''

? ? ? ? }

? ? </script>

3.獲取元素樣式:

<head>

? ? <meta charset="UTF-8">

? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">

? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">

? ? <title>Document</title>

</head>

<body>

? ? <div style="width: 200px;height: 50px;background-color: rebeccapurple;">我是div</div>

? ? <script>

? ? ? ? let div = document.querySelector('div');

? ? ? ? /* 在行內(nèi)樣式上獲取樣式 */

? ? ? ? console.log(div.style.height);

? ? ? ? /* style是獲取不到在行內(nèi)樣式上或者外部樣式上的樣式的 */

? ? ? ? console.log(div.style.color.fontSize);

? ? ? ? /* window.getComputedStyle可以獲取行內(nèi)棺亭,內(nèi)部虎眨,外部所以樣式

? ? ? ? 但是獲得color是rgb格式的,獲取的是background所有格式 */

? ? ? ? console.log((window.getComputedStyle(div,null).fontSize));

? ? ? ? console.log((window.getComputedStyle(div,null).color));

? ? ? ? console.log((window.getComputedStyle(div,null).height));

? ? </script>

獲取元素樣式練習(xí):

<head>

? ? <meta charset="UTF-8">

? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">

? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">

? ? <title>Document</title>

? ? <style>

? ? ? ? .bg{

? ? ? ? ? ? width: 200px;

? ? ? ? ? ? height: 200px;

? ? ? ? ? ? border: 1px solid #ccc;

? ? ? ? }

? ? ? ? .bg2{

? ? ? ? ? ? background-color: red;

? ? ? ? ? ? color: #fff;

? ? ? ? }

? ? </style>

</head>

<body>

? ? <!-- 一個(gè)按鈕點(diǎn)擊一下出現(xiàn)一個(gè)div镶摘,里面寫著我是div嗽桩,

? ? ? ? .bg 是內(nèi)部樣式 有寬200px 高200px 和 1px #CCC 灰色邊框

? ? ? ? 使用JS給div添加上classname bg

? ? ? ? classname.bg2 紅色背景 白字

? ? 鼠標(biāo)移入div顯示.bg2

? ? ?,鼠標(biāo)移出div顯示原來的樣子凄敢,點(diǎn)擊div可以獲取

? ? div的背景色 ?和 color 顏色-->


? ? <button onclick="fn()">點(diǎn)我</button>

? ? <script>

? ? ? ? let btn = document.querySelector('button')

? ? ? ? function fn(){

? ? ? ? ? ? let div = document.createElement('div');

? ? ? ? ? ? let divtext = document.createTextNode('我是div')

? ? ? ? ? ? let body = document.querySelector('body')

? ? ? ? ? ? div.appendChild(divtext);

? ? ? ? ? ? body.appendChild(div)

? ? ? ? ? ? div.className = 'bg';

? ? ? ? ? ? div.onmouseover = function(){

? ? ? ? ? ? div.className = 'bg bg2'

? ? ? ? }

? ? ? ? div.onmouseout = function(){

? ? ? ? ? ? div.className = 'bg'

? ? ? ? }

? ? ? ? div.onmousedown = function(){

? ? ? ? ? ? let b = window.getComputedStyle(div,null).background

? ? ? ? ? ? alert(b)

? ? ? ? }

? ? ? ? }

? ?</script>

4.屬性:

<head>

? ? <meta charset="UTF-8">

? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">

? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">

? ? <title>Document</title>

? ? <style>

? ? ? ? .div1{

? ? ? ? ? ? width: 200px;

? ? ? ? ? ? height: 200px;

? ? ? ? ? ? padding: 10px;

? ? ? ? ? ? margin: 5px;

? ? ? ? ? ? border: 3px solid #ccc;

? ? ? ? ? ? position: absolute;

? ? ? ? ? ? left: 20px;

? ? ? ? ? ? top: 10px;

? ? ? ? }

? ? ? ? .c{

? ? ? ? ? ? width: 100px;

? ? ? ? ? ? height: 100px;

? ? ? ? ? ? background-color: slateblue;

? ? ? ? ? ? position: absolute;

? ? ? ? ? ? top: 30px;

? ? ? ? ? ? left: 30px;

? ? ? ? }

? ? </style>

</head>

<body>

? ? <div class="div1">

? ? ?<div class="c">

? ? <button onclick="getScroll()">獲得滾動(dòng)條頂部的距離</button>

? ? ?</div>

? ? </div>

? ? <script>

? ? ? ? let divDom = document.querySelector('div')

? ? ? ? console.log(window.getComputedStyle(divDom,null).width);

? ? ? ? /*offsetWidth = content 寬度+左右padding+左右border ?*/

? ? ? ? console.log(divDom.offsetWidth);

? ? ? ? /* clientWidth = content 寬度+左右padding 不加border */

? ? ? ? console.log(divDom.clientWidth);


? ? ? ? /* 正常文檔流下 offsetLeft = body的margin-left+divDom的margin-left */

? ? ? ? /* 脫離文檔流的情況下 offsetLeft(25) = divDom的left(20)+divDom的margin-left */

? ? ? ? /* 正常文檔流下offsetTop 就是offsetTop(8) = body 的 margin-top(8) */

? ? ? ? console.log(divDom.offsetLeft);

? ? ? ? console.log(divDom.offsetTop);


? ? ? ? /*offsetParent 自己相對(duì)于(父元素)偏移 */

? ? ? ? console.log(divDom.offsetParent );

? ? ? ? let c = document.getElementsByClassName('c')[0];

? ? ? ? /* c是絕對(duì)定位 是相對(duì)于divDom偏移的 所以他的偏移父元素是divDom */

? ? ? ? console.log(c.offsetParent);

? ? ? ? /* offsetParent 如果子元素的父元素已經(jīng)是定位的元素碌冶,那么他的offsetParent

? ? ? ? 就是已經(jīng)定位(relative,absolute,fixed)的父元素,如果不是就是body */

? ? ? ?function getScroll(){

? ? ? ? ? ?/* 獲取滾動(dòng)條距離頂部的高度 */

? ? ? ? ? ?console.log(document.documentElement.scrollTop);

? ? ? ?}

? ? ? ?/* 滾動(dòng)條滾動(dòng)事件 */

? ? ? ?document.onscroll = function(){

? ? ? ? ? ?console.log(document.documentElement.scrollTop);

? ? ? ?}

? ? </script>

</body>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末涝缝,一起剝皮案震驚了整個(gè)濱河市扑庞,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌拒逮,老刑警劉巖罐氨,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異滩援,居然都是意外死亡栅隐,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來约啊,“玉大人邑遏,你說我怎么就攤上這事∏【兀” “怎么了记盒?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長外傅。 經(jīng)常有香客問我纪吮,道長,這世上最難降的妖魔是什么萎胰? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任碾盟,我火速辦了婚禮,結(jié)果婚禮上技竟,老公的妹妹穿的比我還像新娘冰肴。我一直安慰自己,他們只是感情好榔组,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布熙尉。 她就那樣靜靜地躺著,像睡著了一般搓扯。 火紅的嫁衣襯著肌膚如雪检痰。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天锨推,我揣著相機(jī)與錄音铅歼,去河邊找鬼。 笑死换可,一個(gè)胖子當(dāng)著我的面吹牛椎椰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播锦担,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼俭识,長吁一口氣:“原來是場噩夢啊……” “哼慨削!你這毒婦竟也來了洞渔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤缚态,失蹤者是張志新(化名)和其女友劉穎磁椒,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體玫芦,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡浆熔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了桥帆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片医增。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡慎皱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出叶骨,到底是詐尸還是另有隱情茫多,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布忽刽,位于F島的核電站天揖,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏跪帝。R本人自食惡果不足惜今膊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望伞剑。 院中可真熱鬧斑唬,春花似錦、人聲如沸黎泣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽聘裁。三九已至雪营,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間衡便,已是汗流浹背献起。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留镣陕,地道東北人谴餐。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像呆抑,于是被迫代替她去往敵國和親岂嗓。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • JavaScript中Dom 1.基本概念 1.1 什么是window? window:是一個(gè)全局對(duì)象, 代表瀏覽...
    煤球快到碗里來閱讀 249評(píng)論 0 0
  • <!DOCTYPE html> Leon 序號(hào) 姓名 年齡 性別 ...
    小胖子_d7d8閱讀 187評(píng)論 0 0
  • 十五鹊碍、焦點(diǎn)事件&鍵盤事件 1. 焦點(diǎn)事件 onfocus =>獲得焦點(diǎn)事件 onblur=>失去焦點(diǎn)事件 <...
    默默_01cf閱讀 192評(píng)論 0 0
  • 十六厌殉、其他事件&正則表達(dá)式 1. 阻止事件冒泡 默認(rèn)情況下,觸發(fā)子元素的事件時(shí)侈咕,會(huì)同時(shí)觸發(fā)父元素相同的事件公罕,這就叫...
    默默_01cf閱讀 351評(píng)論 0 0
  • 十四、鼠標(biāo)事件 1. 鼠標(biāo)事件 // 點(diǎn)擊事件 => onclick // 雙擊事件 => ondblclick ...
    默默_01cf閱讀 140評(píng)論 0 0