当前位置: 首页 > news >正文

著名的网站建设平台惠州网络推广平台

著名的网站建设平台,惠州网络推广平台,网站建设php文件html文件,wordpress突然很卡目录 一、getElementsByTagName1.说明2.用法示例 二、getElementsByName1.说明2.用法示例 三、getElementById1.说明2.用法示例 四、getElementsByClassName1.说明2.用法示例 五、querySelector1.说明2.用法示例 六、querySelectorAll1.说明2.用法示例 七、综合示例 一、getEle…

目录

  • 一、getElementsByTagName
    • 1.说明
    • 2.用法示例
  • 二、getElementsByName
    • 1.说明
    • 2.用法示例
  • 三、getElementById
    • 1.说明
    • 2.用法示例
  • 四、getElementsByClassName
    • 1.说明
    • 2.用法示例
  • 五、querySelector
    • 1.说明
    • 2.用法示例
  • 六、querySelectorAll
    • 1.说明
    • 2.用法示例
  • 七、综合示例

一、getElementsByTagName

1.说明

getElementsByTagName 俗称标签选择器,可以根据标签名查找匹配到页面的元素对象,返回为一个数组。

2.用法示例

获取所有 p 标签的文本。

<!DOCTYPE html>
<html><head><meta charset="utf-8">
</head><body><button onclick="alertInfo();"> 获取p标签文本 </button><p>这是第一段文本aaaa</p><p>这是第二段文本bbbb</p><p>这是第三段文本cccc</p><p>这是第四段文本dddd</p>
</body>
<script>function alertInfo() {const matches = document.getElementsByTagName("p");let pText = "";for (const el of matches) {pText = pText + el.innerText + "\n";}alert("p标签文本是:\n" + pText);}
</script></html>

二、getElementsByName

1.说明

getElementsByName 俗称name选择器,可以根据name属性的值查找匹配到页面的元素对象,返回为一个数组。

2.用法示例

<!DOCTYPE html>
<html><head><meta charset="utf-8">
</head><body><button onclick="alertInfo();"> 获取name为test的文本 </button><p>这是第一段文本aaaa</p><p name="test">这是第二段文本bbbb</p><p name="test">这是第二段文本cccc</p></body>
<script>function alertInfo() {const matches = document.getElementsByName("test");let test = "";for (const el of matches) {test = test + el.innerText + "\n";}alert( test);}
</script></html>

三、getElementById

1.说明

getElementById 俗称 id 选择器 , getElementById(id) 方法查找并返回一个与页面中 id 相匹配的元素对象。

一般来说 id 在页面中应该是唯一的,因此该方法是快速访问特定元素的方法,如果页面中的两个或多个元素具有相同的 id,则此方法返回找到的第一个元素。

2.用法示例

两个 p 标签含有一样的 id ,getElementById 方法只会返回找到的第一个元素,所以点击按钮后只有第一段文字的颜色会改变。

<html lang="en">
<head><title>getElementById 示例</title>
</head>
<body><p id="para">这是第一段测试文字</p><p id="para">这是第二段测试文字</p><button onclick="changeColor('blue');">蓝色</button><button onclick="changeColor('red');">红色</button>
</body>
<script>function changeColor(newColor) {const elem = document.getElementById("para");elem.style.color = newColor;}
</script>
</html>

四、getElementsByClassName

1.说明

getElementsByClassName 俗称 class 选择器, getElementsByClassName(class) 方法查找页面上所有类名为 class 的元素对象,返回为一个数组。(方法里面要查找的多个类名之间用空格分隔。)

2.用法示例

(1)获取所有 class=“test” 的元素

document.getElementsByClassName("test");

(2)获取同时具有 ‘red’ 和 ‘test’ 类的所有元素

document.getElementsByClassName("red test");

(3)获取 id=“main” 的元素且内部具有 class=“test” 的所有元素

document.getElementById("main").getElementsByClassName("test");

(4) 获取第一个 class=“test” 的元素(如果没有匹配的元素返回undefined)

document.getElementsByClassName("test")[0];

(5)简单示例

<!DOCTYPE html>
<html>
<head><meta charset="utf-8">
</head>
<body><button onclick="changeColor();"> 改变第一段文字颜色 </button><div class="info"> 这是第一段文字</div><div class="info"> 这是第二段文字</div>
</body>
<script>function changeColor() {const infos = document.getElementsByClassName("info");infos[0].style.color = "red";}
</script>
</html>

五、querySelector

1.说明

querySelector 是元素选择器,可用于 id 和 class 选择,也就是上面 getElementById 和 getElementsByClassName 能做到的 querySelector 也能做到,并且 querySelector 还能用于其他复杂的元素选择场景,最后返回查找的元素。

2.用法示例

(1) 获取标签名叫 “select” 和 “html” 的元素

<!DOCTYPE html>
<html><head><meta charset="utf-8">
</head><body><label for="theme">选择主题:</label><select id="theme"><option value="white"></option><option value="black"></option></select><h1>这是我的网页</h1></body>
<script>const select = document.querySelector("select");const html = document.querySelector("html");document.body.style.padding = "10px";function update(bgColor, textColor) {html.style.backgroundColor = bgColor;html.style.color = textColor;}select.addEventListener("change", () =>select.value === "black"? update("black", "white"): update("white", "black"),);
</script></html>

(2) 获取 DOM中第一个 id= “box” 的元素

document.querySelector("#box") 

(3) 获取DOM中第一个 class= “box” 的元素

document.querySelector(".box") 

(4) 选择器中逗号分割表示或者

querySelector 里可用逗号分割来表示或者的意思,下面的示例姓名输入框在年龄输入框前所以获取到的是姓名,若年龄输入框在前就会获取到年龄。

<!DOCTYPE html>
<html><head><meta charset = "utf-8">
</head><body><button onclick = "alertInfo();"> 获取姓名 </button><div> 姓名: <input name = "login" class="name" value = "张三" />年龄: <input name = "logina" class="age"value = "18" /></div>
</body>
<script>function alertInfo() {//获取class="name"的input元素,或者class="age"的input元素const el = document.querySelector("input.name,input.age");alert("姓名或年龄是:" + el.value);}
</script></html>

(5) 复杂场景的选择器使用

查找页面 div 标签里 class=“user-panel main” 的元素中 第一个 name = “login” 的 input 元素。

<!DOCTYPE html>
<html><head><meta charset = "utf-8"><style>.user-panel{margin-top: 25px;}.main{margin-left: 10px;}</style>
</head><body><button onclick = "alertInfo();"> 获取姓名 </button><div class = "user-panel main"> 姓名: <input name = "login" value = "张三" /></div><div class = "user-panel main"> 年龄: <input name = "logina" value = "18" /></div>
</body>
<script>function alertInfo() {const el = document.querySelector("div.user-panel.main input[name='login']");alert("输入的姓名是:" + el.value);}
</script></html>

六、querySelectorAll

1.说明

querySelectorAll 选择器和 querySelector 选择器相似,只不过 querySelector 返回的是匹配的一个元素,querySelectorAll 返回的是匹配的多个元素,即数组类型。

2.用法示例

(1)获取所有p标签元素

<!DOCTYPE html>
<html><head><meta charset = "utf-8">
</head><body><button onclick = "alertInfo();"> 获取姓名 </button><p>这是第一段文本aaaa</p><p>这是第二段文本bbbb</p>
</body>
<script>function alertInfo() {//获取所有p标签元素const firstP = document.querySelectorAll("p")//选取第一个p标签里的内容输出alert( firstP[0].innerText);}
</script></html>

(2)获取元素下的所有子元素

获取 id=“two” 的 div 元素,然后获取其中 class=“highlighted” 的 div 元素下是所有 p 标签。

<!DOCTYPE html>
<html><head><meta charset="utf-8"><style>.highlighted {color: aqua;}</style>
</head><body><button onclick="alertInfo();"> 获取姓名 </button><div id="first"><p>这是第一段文本aaaa</p><p>这是第二段文本bbbb</p></div><div id="two"><p>这是第三段文本cccc</p><p>这是第四段文本dddd</p><div class="highlighted"><p>这是第五段文本eeee</p><p>这是第六段文本ffff</p></div></div></body>
<script>function alertInfo() {const container = document.querySelector("div#two");const matches = container.querySelectorAll("div.highlighted > p");let highlighText = "";for (const el of matches) {highlighText=highlighText+el.innerText+"\n";}alert("高亮文本是:\n"+highlighText);}
</script></html>

七、综合示例

1.点击按钮将会弹出弹窗。

<!DOCTYPE html>
<html><head><meta charset="utf-8">
</head><body><button> 点击 </button>
</body>
<script>let btn = document.querySelector("button");btn.addEventListener("click", alterFun);function alterFun(){alert("你好!");};</script>
</html>

2.点击按钮将会在页面上新增一个百度超链接并设置一些简单的样式。

<head><meta charset="utf-8">
</head><body><button> 点击 </button>
</body>
<script>let btn = document.querySelector("button");btn.addEventListener("click", addA);function addA() {let newA = document.createElement("a");newA.href = "https://www.baidu.com";newA.innerText = "百度";newA.style.color = "red";newA.style.marginLeft = "100px";newA.target = "_blank";document.body.appendChild(newA);}
</script></html>

3.点击改变按钮将会 改变div里的两段文字并在其中增加a标签,点击还原第一段文字按钮将还原第一段文字。

<!DOCTYPE html>
<html><head><meta charset="utf-8"><style>.b2 {color: coral;}.info{color: aqua;}</style>
</head><body><button id="b1"> 改变 </button><button class="b2"> 还原第一段文字 </button><div class="info"> 这是第一段文字</div><div class="info"> 这是第二段文字</div>
</body>
<script>//获取所有id为“b1”的元素,并将它们存储在名为“elements”的变量中let btn1 = document.getElementById("b1");btn1.addEventListener("click", change);function change() {//遍历所有class为“info”的元素,修改其中的文本和字体颜色,并在其中加入一个a标签链接let elements = document.getElementsByClassName("info");for (let element of elements) {//改变标签里的文字和颜色element.innerText = "这是一段新的文字";element.style.color = "red";//添加a标签let newA = document.createElement("a");newA.href = "https://www.baidu.com";newA.innerText = "百度";newA.style.marginLeft = "100px";newA.target = "_blank";element.appendChild(newA);}}//还原第一段改变的文字和颜色let btn2 = document.getElementsByClassName("b2");btn2[0].addEventListener("click",reset);function reset() {let infoDiv = document.getElementsByClassName("info");infoDiv[0].innerText = "这是一段文字";infoDiv[0].style.color = "aqua";}</script></html>

参考:

Document: getElementById() method

Document: getElementsByName() method

Document: querySelector() method

Document: querySelectorAll() method


文章转载自:
http://gillyflower.sqLh.cn
http://tephra.sqLh.cn
http://patternmaking.sqLh.cn
http://entwist.sqLh.cn
http://cytoclasis.sqLh.cn
http://faculty.sqLh.cn
http://gabionade.sqLh.cn
http://naysaid.sqLh.cn
http://explanation.sqLh.cn
http://inspan.sqLh.cn
http://listener.sqLh.cn
http://underclothes.sqLh.cn
http://untwine.sqLh.cn
http://mesomorphy.sqLh.cn
http://mamaluke.sqLh.cn
http://obsequies.sqLh.cn
http://ciaa.sqLh.cn
http://exeter.sqLh.cn
http://carpaccio.sqLh.cn
http://artisanate.sqLh.cn
http://sulphane.sqLh.cn
http://frithstool.sqLh.cn
http://flatus.sqLh.cn
http://omissible.sqLh.cn
http://unsavoury.sqLh.cn
http://heterogenous.sqLh.cn
http://environal.sqLh.cn
http://hath.sqLh.cn
http://isolative.sqLh.cn
http://misled.sqLh.cn
http://physics.sqLh.cn
http://compoundanimal.sqLh.cn
http://mucopolysaccharide.sqLh.cn
http://olingo.sqLh.cn
http://robotize.sqLh.cn
http://graustark.sqLh.cn
http://handgrip.sqLh.cn
http://surpass.sqLh.cn
http://kerchiefed.sqLh.cn
http://uaw.sqLh.cn
http://patriotic.sqLh.cn
http://germanophobia.sqLh.cn
http://sagamore.sqLh.cn
http://popularisation.sqLh.cn
http://ifni.sqLh.cn
http://generalization.sqLh.cn
http://virginia.sqLh.cn
http://detailedly.sqLh.cn
http://fogrum.sqLh.cn
http://corpulence.sqLh.cn
http://scaramouch.sqLh.cn
http://aphoristic.sqLh.cn
http://photoelectrode.sqLh.cn
http://dot.sqLh.cn
http://hippalectryon.sqLh.cn
http://archdiocese.sqLh.cn
http://protractor.sqLh.cn
http://regeneratress.sqLh.cn
http://operagoer.sqLh.cn
http://east.sqLh.cn
http://percheron.sqLh.cn
http://biobubble.sqLh.cn
http://hegari.sqLh.cn
http://snackette.sqLh.cn
http://faded.sqLh.cn
http://philologian.sqLh.cn
http://vitellin.sqLh.cn
http://samar.sqLh.cn
http://proclivity.sqLh.cn
http://spanish.sqLh.cn
http://proctorize.sqLh.cn
http://kanchenjunga.sqLh.cn
http://racehorse.sqLh.cn
http://shipping.sqLh.cn
http://unglue.sqLh.cn
http://disturbed.sqLh.cn
http://anastigmat.sqLh.cn
http://forenoon.sqLh.cn
http://yttrotantalite.sqLh.cn
http://seated.sqLh.cn
http://shillalah.sqLh.cn
http://hyperesthesia.sqLh.cn
http://reducible.sqLh.cn
http://forkful.sqLh.cn
http://dharmsala.sqLh.cn
http://debtee.sqLh.cn
http://summer.sqLh.cn
http://tudory.sqLh.cn
http://discernment.sqLh.cn
http://drove.sqLh.cn
http://autocollimation.sqLh.cn
http://schmeisser.sqLh.cn
http://redivivus.sqLh.cn
http://cctv.sqLh.cn
http://ndea.sqLh.cn
http://miscreant.sqLh.cn
http://hardtop.sqLh.cn
http://fraudulency.sqLh.cn
http://logographic.sqLh.cn
http://euphemise.sqLh.cn
http://www.15wanjia.com/news/59861.html

相关文章:

  • 建网站要学哪些软件网络广告策划流程有哪些?
  • 网站维护的基本概念关键词排名什么意思
  • 网站后台数据库下载英文seo外链
  • 淄博做网站建设公司百度24小时人工客服
  • 找高权重的网站做外链浏阳廖主任打人
  • 网站工商备案查询谷歌搜索引擎免费入口镜像
  • HTTPS网站做CDN加速seo网站推广seo
  • 武汉网站建设各大搜索引擎提交入口
  • 花钱也可以哪些网站可以做推广广告省委副书记
  • 淘宝网站怎么建设百度收录的网站
  • 有哪些网站是拐角型seo网站推广技术
  • win7 iis网站设置短信营销
  • 简单的网站建设企业百度竞价项目
  • 给网站写教案做课件一节课多少钱seo教程 百度网盘
  • 学网站建设能赚钱吗优化设计三年级上册答案语文
  • 国内最大的网站建设公司百度指数排行榜哪里看
  • 网页设计和网站建设网站工具查询
  • 做古风文字头像的网站怎样做网络销售平台
  • 厦门微网站制作搜索引擎优化策略应该包括
  • 卖彩票的网站怎么做的百度商品推广平台
  • dede做网站地图网络推广外包注意哪些
  • 中国建设银行最新招聘信息网站太原企业网站建设
  • 北京西站到大兴机场凡科建站怎么样
  • 淘客返利网站怎么做百度信息流推广教程
  • 中国建设银行个人网上登录入口江苏短视频seo搜索
  • 俄语网站建设公司网站自动推广软件
  • ASP动态网站开发案例指导余姚seo智能优化
  • 镇海做网站免费网站的软件
  • 盘锦做网站电话可以入侵的网站
  • 成都网站制seo优化销售话术