pythonjs设置_python dom操作
點我
常見事件
(2)對標(biāo)簽值的操作
innerText
//設(shè)置文本
var oDiv=document.getElementById('box');
oDiv.innerText='哈哈哈'
innerHTML
//既可以設(shè)置文本 又可以設(shè)置標(biāo)簽 設(shè)置 set 獲取 get 點語法(set方法和get方法)
oDiv.innerHTML='
嘿嘿嘿
'(3) 獲取標(biāo)簽內(nèi)容值
//只獲取所有(當(dāng)前標(biāo)簽和子標(biāo)簽)文本內(nèi)容
console.log(oDiv.innerText);//獲取父標(biāo)簽中所有標(biāo)簽元素(子標(biāo)簽,空格,換行,文本)
console.log(oDiv.innerHTML);
(4)對表單控件value的操作
//設(shè)置value值 只適應(yīng)于表單控件元素
document.getElementById('user').value = 'alex';
console.log(document.getElementById('user').value);
(5)對標(biāo)簽的css操作(樣式操作)
xxx.style.css的屬性key='值'
盒子顏色的切換
var oDiv = document.getElementsByClassName('box')[0];var isRed = true;
oDiv.οnclick= function() {if(isRed) {//this 誰做的事件操作 this指向誰
this.style.backgroundColor = 'green';this.style.width = '400px';
isRed= false;
}else{this.style.backgroundColor = 'red';
isRed= true;
}
}
(6)對標(biāo)簽屬性的操作
var oDiv = document.getElementsByClassName("box")[0];var oBtn = document.getElementById("btn");var isShow = true;
oBtn.οnclick= function() {if(isShow){
oDiv.id= "box1";
oDiv.title= "hhhh";
console.log(oDiv.className);//box
oDiv.className = oDiv.className+"active";
isShow= false}else{
oDiv.className= "box";
isShow= true}
}
DOM的操作(創(chuàng)建,追加,刪除)
js中的面向?qū)ο?/p>
定時器
1. DOM的操作(創(chuàng)建,追加,刪除)
(1)DOM節(jié)點的獲取
parentNode 獲取父級標(biāo)簽
nextElementSibling 獲取下一個兄弟節(jié)點
children 獲取所有的子標(biāo)簽
var oP = document.getElementById("wuxia");
console.log(oP.parentNode);//父級div標(biāo)簽下的所有內(nèi)容
console.log(oP.nextElementSibling.innerText); //蕭峰
console.log(oP.nextSibling.innerText); //IE瀏覽器
var a = oP.nextElementSibling||oP.nextSibling;
console.log(a);
console.log(oP.parentNode.childen);
(2) DOM的增刪操作
創(chuàng)建 createElement()
//既可以創(chuàng)建已有的標(biāo)簽,還可以創(chuàng)建自定義的標(biāo)簽
var oDiv = document.createElement('div')
追加 appendChild() 父子標(biāo)簽操作
父.appendChild(oDiv);
刪除 removeChild()
父.removeChild(子節(jié)點);
自己.parentNode.removeChild(自己)
插入insertBefore()
父.insertBefore(新的子節(jié)點,要參考的節(jié)點)
利用創(chuàng)建刪除實現(xiàn)隱藏代碼示例
var oBtn = document.querySelector("#btn");var oDel = document.querySelector("#del");var oFather = document.querySelector(".father");var oP = null;var oA = null;
oBtn.οnclick= function(){//創(chuàng)建 dom p標(biāo)簽
oP = document.createElement("p");
oA= document.createElement("a");//追加到父級標(biāo)簽中
oFather.appendChild(oP);
oFather.insertBefore(oA,oP);//設(shè)置對象屬性值
oA.href = 'http://www.baidu.com';//設(shè)置值
oA.innerText="百度";
oP.innerHTML= "alex";
oP.setAttribute("class","active")
};
oDel.οnclick= function(){//如果oP對象存在就結(jié)束程序
if (!oP){return;// }else{
console.log(oFather);//將增加的p標(biāo)簽移出
oFather.removeChild(oP)
}
}
使用js模擬hover選擇器
Title*{
padding:0;
margin:0;
}
ul{
list-style: none;
}
ul{
width: 600px;
height: 80px;
line-height: 80px;
text-align:center;
overflow:hidden;
}
ul li{float:left;
margin:010px;
width: 80px;
height: 80px;
background: darkturquoise;
color: #fff;
}
ul li.active{
background: red;
}
舉 頭 望 明 月var lists = document.getElementsByTagName("li");for(var i =0;i
lists[i].οnclick= function(){//在修改當(dāng)前盒子樣式之前,現(xiàn)將所有盒子的類名置空
for(var j=0; j
lists[j].className="";
}this.className = "active"; //修改當(dāng)前 鼠標(biāo)進(jìn)入盒子的樣式
}
}
選項卡
Title*{
padding:0;
margin:0;
}
ul{
list-style: none;
}
#tab{
width: 480px;
margin: 20px auto;
border: 1px solid red;
}
ul{
width:100%;
overflow: hidden;
}
ul li{float: left;
width: 160px;
height: 60px;
line-height: 60px;
text-align: center;
background-color: #cccccc;
}
ul li a{
text-decoration: none;
color:black;
}
li.active {
background-color: darkcyan;
}
p{
display: none;
height: 200px;
text-align: center;
line-height: 200px;
background-color: darkgrey;
}
p.active{
display: block;
}
首頁
新聞
圖片
首頁內(nèi)容
新聞內(nèi)容
圖片內(nèi)容
var lists = document.getElementsByTagName("li");var oPs = document.getElementsByTagName("P");//var i;
//i=3 變量提升 會導(dǎo)致 變量 是一個全局作用域
//var 聲明變量 全局作用域,存在變量提升
for(var i= 0; i
lists[i].currentIndex =i;
console.dir(lists[i]);
lists[i].οnclick= function(){for(var j = 0; j
lists[j].className= "";
oPs[j].className= '';
}this.className = "active";
oPs[this.currentIndex].className = 'active'}
}
2.js中的面向?qū)ο?/p>
(1)使用Object或?qū)ο笞置媪縿?chuàng)建對象
//構(gòu)造函數(shù)方式創(chuàng)建對象
let person = newObject();
person.name= 'alex';
person.fav= function() {
console.log(this);
}//字面量方式創(chuàng)建 使用最多
var person2 ={
name:'wusir',
age:19,
fav:function() {
alert(this.age);
}
}
person2.fav();
3.定時器
事件點擊案例
Title提交
確認(rèn)
大海啊,全是水
functionf1(ths) {
ths.style.color= 'red';vardiv1=document.getElementById('xxx');
div1.style.color= 'yellow';
}varbtn2=document.getElementById('btn2');
btn2.οnclick= function() {this.style.color= 'blue';
}
總結(jié)
以上是生活随笔為你收集整理的pythonjs设置_python dom操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python处理完数据导入数据库_pyt
- 下一篇: 文档自动排序长短_css 文档流