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

网站制作的一般步骤网站的搜索引擎

网站制作的一般步骤,网站的搜索引擎,seo推广原理,杭州专门做网站如果你希望访问 HTML 页面中的任何元素&#xff0c;那么您总是从访问 document 对象开始&#xff01; 查找HTML元素 document.getElementById(id) 通过元素 id 来查找元素 <!DOCTYPE html> <html> <head><meta charset…

如果你希望访问 HTML 页面中的任何元素,那么您总是从访问 document 对象开始!

查找HTML元素

document.getElementById(id)                                通过元素 id 来查找元素
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<div id="myDiv">Hello, world!</div><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var myDiv = document.getElementById("myDiv");console.log(myDiv);  // 输出 <div id="myDiv">Hello, world!</div>
</script>
</body>
</html>
document.getElementsByTagName(name)          通过标签名来查找元素

这个方法接收一个参数 name,该参数是一个字符串,表示你想要选择的元素的标签名称

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<p>一段话</p>
<p>一段话1</p>
<p>一段话2</p><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var p = document.getElementsByTagName("p");console.log(p);console.log(p[1]);
</script>
</body>
</html>

返回的 NodeList 对象是一个“类数组”,可以像数组一样使用。你可以通过索引访问特定的元素,例如,document.getElementsByTagName("p")[0] 将返回第一个 <p> 元素

需要注意的是,NodeList 是“静态的”,也就是说,它并不会反映后续对文档的修改。如果在后续对文档进行了修改(例如,添加或删除了元素),那么你需要再次调用 getElementsByTagName 来获取最新的元素(查找HTML元素的方法的性质都类似,类比即可!)

document.getElementsByClassName(name)通过类名来查找元素
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<button class="btn">我是按钮</button><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var btn = document.getElementsByClassName("btn");console.log(btn);
</script>
</body>
</html>
document.querySelector(CSS选择器)                                        通过CSS选择器选择一个元素
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<button class="btn">我是按钮</button><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var btn = document.querySelector(".btn");console.log(btn);
</script>
</body>
</html>
document.querySelectorAll(CSS选择器)                                    通过CSS选择器选择多个元素
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<ul class="list"><li>列表项1</li><li>列表项2</li><li>列表项3</li><li>列表项4</li>
</ul><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var list = document.querySelectorAll(".list li");console.log(list);
</script>
</body>
</html>

获取HTML元素的值

元素节点.innerText获取 HTML元素的 inner Text
元素节点.innerHTIML获取 HTML元素的 inner HTML
元素节点.属性获取 HTIML 元素的属性值
元素节点getAttribute(attribute)获取 HTIML 元素的属性值
元素节点style.样式获取 HTML 元素的行内样式值
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<a ID="a" href="https://www.csdn.net/">CSDN</a><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var a = document.getElementById("a");console.log(a.innerText);
</script>
</body>
</html>

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<div id="box"><h1>我是Box中的大标题</h1>
</div><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var box = document.getElementById("box");console.log(box.innerHTML);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<a ID="a" href="https://www.csdn.net/">CSDN</a><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var a = document.getElementById("a");console.log(a.href);console.log(a.getAttribute("href");console.log(a.getAttribute(""));
<!-- 创建一个超链接,默认为空,设置href属性为https://www.csdn.net/ ,使用JavaScript代码读取href属性-->
</script>
</body>
</html>

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<div style="width: 100px;height: 100px;background: red;" id="box"></div><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var box = document.getElementById("box");console.log(box.style.width);
</script>
</body>
</html>

小知识:

如果CSS的样式名中含有-,这种名称在JS中是不合法的比如background-color,需要将这种样式名修改为驼峰命名法,去掉-,然后将 - 后的字母大写,我们通过style属性设置的样式都是行内样式,同样的获取也是行内样式,而行内样式有较高的优先级,所以通过JS修改的样式往往会立即显示,但是如果在样式中写了!important,则此时样式会有最高的优先级,即使通过JS也不能覆盖该样式,此时将会导致JS修改样式失效,所以尽量不要为样式添加!important

改变HTML的值

元素节点.innerText = new text content改变元素的 inner Text
元素节点.innerHTML = new html content改变元素的 inner HTML
元素节点.属性 = new value改变 HTML 元素的属性值
元素节点.setAttribute(attributevalue)改变 HTML 元素的属性值
元素节点.style.样式 = new style改变 HTML 元素的行内样式值
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<button id="btn">我是按钮</button><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var btn = document.getElementById("btn");btn.innerText = "我是JavaScript的按钮";console.log(btn);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<div id="box"></div><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var box = document.getElementById("box");box.innerHTML = "<h1>我是Box中的大标题</h1>";console.log(box);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<a id="a" href="">打开百度,你就知道!</a><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var a = document.getElementById("a");a.href="https://www.baidu.com";console.log(a);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<div style="width: 100px;height: 100px;background: red;" id="box"></div><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var box = document.getElementById("box");box.style.background = "green";console.log(box);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<div style="width: 100px;height: 100px;background: red;" id="box"></div><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var box = document.getElementById("box");box.style.background = "green";console.log(box);
</script>
</body>
</html>

添加HTML元素 

document.createElement(element)创建 HTML 元素节点
document.createAttribute(attribute)创建 HTML 属性节点
document.createTextNode(text)创建 HTML 文本节点
元素节点.removeChild(element)删除 HTML 元素
元素节点.appendChild(element)添加 HTML 元素
元素节点.replaceChild(element)替换 HTML 元素
元素节点.insertBefore(element)在指定的子节点前面插入新的子节点

创建不代表是添加到网页上了!!!

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var a = document.createElement("a");a.href = "https://www.csdn.net";  // 设置链接地址var text = document.createTextNode("CSDN");a.appendChild(text);  // 将链接文本添加到链接元素中document.getElementsByTagName("body")[0].appendChild(a);var ul = document.createElement("ul");var li1 = document.createElement("li");var text1 = document.createTextNode("列表项1");li1.appendChild(text1);ul.appendChild(li1);var li2 = document.createElement("li");var text2 = document.createTextNode("列表项2");li2.appendChild(text2);ul.appendChild(li2);var li3 = document.createElement("li");var text3 = document.createTextNode("列表项3");li3.appendChild(text3);ul.appendChild(li3);var li4 = document.createElement("li");var text4 = document.createTextNode("列表项4");li4.appendChild(text4);ul.appendChild(li4);var li5 = document.createElement("li");var text5 = document.createTextNode("列表项5");li5.appendChild(text5);ul.appendChild(li5);document.getElementsByTagName("body")[0].appendChild(ul);
</script>
</body>
</html>

创建一个ul列表,里边有四个li子元素,删除第一个li,替换最后一个li

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title></title>
</head>
<body>
<ul id="ul"><li id="first">列表项1</li><li>列表项2</li><li>列表项3</li><li id="last">列表项4</li>
</ul><!-- 在这里写JavaScript代码,因为JavaScript是由上到下执行的 -->
<script>var ul = document.getElementById("ul");var first = document.getElementById("first");var last = document.getElementById("last");/*删除第一个*/ul.removeChild(first);/*替换最后一个*/var replaceLi = document.createElement("li");replaceLi.innerHTML = "列表4的替换";ul.replaceChild(replaceLi, last);
</script>
</body>
</html>

查找HTML父子元素节点

元素节点.parentNode返回元素的父节点
元素节点.parentElement返回元素的父元素
元素节点.childNodes返回元素的一个子节点的数组(包含空白文本Text节点)
元素节点.children返回元素的一个子元素的集合(不包含空白文本Text节点)
元素节点.firstChild返回元素的第一个子节点(包含空白文本Text节点)
元素节点.firstElementChild返回元素的第一个子元素(不包含空白文本Text节点)
元素节点.lastChild返回元素的最后一个子节点(包含空白文本Text节点)
元素节点.lastElementChild返回元素的最后一个子元素(不包含空白文本Text节点)
元素节点.previousSibling返回某个元素紧接之前节点(包含空白文本Text节点)
元素节点.previousElementSibling返回指定元素的前一个兄弟元素(相同节点树层中的前一个元素节点)
元素节点.nextSibling返回某个元素紧接之后节点(包含空白文本Text节点)
元素节点.nextElementSibling返回指定元素的后一个兄弟元素(相同节点树层中的下一个元素节点)

文章转载自:
http://beingless.rwzc.cn
http://tensility.rwzc.cn
http://mescal.rwzc.cn
http://quinquefoliolate.rwzc.cn
http://annamese.rwzc.cn
http://countryroad.rwzc.cn
http://vicinage.rwzc.cn
http://blastissimo.rwzc.cn
http://groan.rwzc.cn
http://paraboloid.rwzc.cn
http://inordinate.rwzc.cn
http://mechanist.rwzc.cn
http://misbirth.rwzc.cn
http://languorous.rwzc.cn
http://rollock.rwzc.cn
http://sordid.rwzc.cn
http://caparison.rwzc.cn
http://mousseline.rwzc.cn
http://schitz.rwzc.cn
http://inanimation.rwzc.cn
http://begotten.rwzc.cn
http://thermopylae.rwzc.cn
http://fls.rwzc.cn
http://bulldyker.rwzc.cn
http://zek.rwzc.cn
http://estral.rwzc.cn
http://alphosis.rwzc.cn
http://malevolence.rwzc.cn
http://ier.rwzc.cn
http://yourselves.rwzc.cn
http://impressive.rwzc.cn
http://deutzia.rwzc.cn
http://demount.rwzc.cn
http://gastrologist.rwzc.cn
http://mantelletta.rwzc.cn
http://significans.rwzc.cn
http://merciless.rwzc.cn
http://erinyes.rwzc.cn
http://definable.rwzc.cn
http://hyperbola.rwzc.cn
http://unsexed.rwzc.cn
http://boffo.rwzc.cn
http://darpanet.rwzc.cn
http://morganatic.rwzc.cn
http://mre.rwzc.cn
http://pdd.rwzc.cn
http://insomnious.rwzc.cn
http://withstand.rwzc.cn
http://septic.rwzc.cn
http://flamdoodle.rwzc.cn
http://roentgenogram.rwzc.cn
http://reeve.rwzc.cn
http://protractile.rwzc.cn
http://typewriter.rwzc.cn
http://inflexibility.rwzc.cn
http://reserpinized.rwzc.cn
http://privateering.rwzc.cn
http://mayst.rwzc.cn
http://liquefactive.rwzc.cn
http://eugeosyncline.rwzc.cn
http://aliquant.rwzc.cn
http://ehv.rwzc.cn
http://godson.rwzc.cn
http://jutish.rwzc.cn
http://deracialize.rwzc.cn
http://oswald.rwzc.cn
http://pembrokeshire.rwzc.cn
http://pony.rwzc.cn
http://kuromaku.rwzc.cn
http://maximin.rwzc.cn
http://illiteracy.rwzc.cn
http://unhappily.rwzc.cn
http://super.rwzc.cn
http://vesiculate.rwzc.cn
http://his.rwzc.cn
http://usps.rwzc.cn
http://autocontrol.rwzc.cn
http://putrilage.rwzc.cn
http://wyvern.rwzc.cn
http://nopal.rwzc.cn
http://thereat.rwzc.cn
http://mucosity.rwzc.cn
http://kitenge.rwzc.cn
http://sexuality.rwzc.cn
http://swingometer.rwzc.cn
http://quinol.rwzc.cn
http://infantility.rwzc.cn
http://incontinent.rwzc.cn
http://anencephalic.rwzc.cn
http://backhouse.rwzc.cn
http://convertiplane.rwzc.cn
http://contrail.rwzc.cn
http://technicality.rwzc.cn
http://sejant.rwzc.cn
http://wcc.rwzc.cn
http://preadult.rwzc.cn
http://lucent.rwzc.cn
http://village.rwzc.cn
http://conservatorium.rwzc.cn
http://weariness.rwzc.cn
http://www.hrbkazy.com/news/63210.html

相关文章:

  • 网站制作公司北京网站建设公司电商大数据查询平台免费
  • 网站建设亿玛酷技术抖音权重查询
  • 网站的国际化 怎么做如何制作一个网站
  • 自己做网站在线看pdf网络营销推广案例
  • 美国视频网站宽带费用百度平台联系方式
  • 顺企网官网下载安装宜昌网站seo收费
  • web记事本做网站怎么改变字的颜色视频外链平台
  • 合肥市网站优化济南网络推广公司电话
  • 沈阳高端做网站建设最新新闻国内大事件
  • 织梦做网站如何套取别人网站的模板zac博客seo
  • 易班网站建设基础深圳市seo网络推广哪家好
  • 基础建设的网站有哪些湘潭关键词优化服务
  • 湖北企业网站优化排名手机上可以创建网站吗
  • WordPress 标签 模板seo关键词怎么选
  • 做电子商务网站的公司品牌推广公司
  • 专做外贸的网站最彻底的手机优化软件
  • 竞价页面网站做优化武汉百度开户代理
  • 中国建筑考试网官网首页重庆seo网站哪家好
  • 动态网站建设常见的4种技术营销型网站设计
  • 涿州做网站公司站长之家收录查询
  • 搭建一个商城网站不收费的小说网站排名
  • thinkphp网站开发哪里有竞价推广托管
  • 群晖外网打开wordpress山东seo
  • 资源网站2345网址大全下载到桌面
  • 有没有做软件的外包网站优化大师电视版
  • 城阳做网站找哪家好搜索关键词排名优化
  • 天猫旗舰店网站建设案例网站seo系统
  • 西安企业网站设计制作网络推广计划制定步骤
  • 网站里的活动专题栏怎么做百度推广登录后台
  • wordpress模板代码编辑插件搜索引擎优化的完整过程