2.1.1 ID 选择器

						
2.1.1  ID 选择器 

JavaScript 提供了原生方法getElementById(),实现在DOM中选择指定ID的元素

var element = document.getElementById("id");

<html>
<div id="aaaa">测试1</div>
<div id="bbbb">测试2</div>
<div id="cccc">测试3</div>

</html>

<script>
var element = document.getElementById("id");
console.log(element)
</script>


node2:/var/www/html#cat m12.html 
<html>
<div id="aaaa">测试1</div>
<div id="bbbb">测试2</div>
<div id="cccc">测试3</div>

</html>

<script>
var element = document.getElementById("aaaa");
console.log(element)
console.log(element.innerHTML)
</script>


jQuery("#id")

参数id为字符串,表示标签的id属性值,返回值为包含匹配id的元素的jQuery对象

<script src="jquery-1.6.4.js" type="text/javascript"></script>
<html>
<div id="aaaa">测试1</div>
<div id="bbbb">测试2</div>
<div id="cccc">测试3</div>

</html>
<script>
$(function(){

$("#aaaa").css("background","red")
console.log($("#aaaa"))

})

</script>

<script src="jquery-1.6.4.js" type="text/javascript"></script>
<html>
<div id="aaaa">测试1</div>
<div id="bbbb">测试2</div>
<div id="cccc">测试3</div>

</html>
<script>
$(function(){

$("#aaaa").css("background","red")
console.log($("#aaaa")[0].innerHTML)

})

</script>


在上面代码中,$("#aaaa")函数包含的#aaaa参数就表示ID选择器,jQuery构造器能根据这个选择器

准确定位到DOM中该元素的位置,并返回包含该元素引用的jQuery对象
发布了3754 篇原创文章 · 获赞 112 · 访问量 346万+

猜你喜欢

转载自blog.csdn.net/zhaoyangjian724/article/details/103852318