<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQueryID选择器</title>
<style type="text/css">
div{
width: 100px;
height: 100px;
}
</style>
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){
// 匹配ID为div1的元素,设置背景色为#999
$('#div1').css('background','#999');
// $('#a\\.b').css('background','#111');
// $('#a\\:b').css('background','#222');
// $('#\\[div\\]').css('background','#333');
// $('#a\\.b').css('color','#fff');
// $('#a\\:b').css('color','#fff');
// $('#\\[div\\]').css('color','#fff');
//document.getElementById()方法,特殊的字符可以不需要进行转义
var div1 = document.getElementById('a.b');
div1.style.backgroundColor = '#111';
div1.style.color = '#fff';
var div2 = document.getElementById('a:b');
div2.style.backgroundColor = '#222';
div2.style.color = '#fff';
var div3 = document.getElementById('[div]');
div3.style.backgroundColor = '#333';
div3.style.color = '#fff';
});
// 注意:jQuery需要对参数字符串进行解析,并匹配参数值,然后再调用getElementById()方法获取ID元素,所以话费时间会成倍增长.因此,在不是很必须的前提下,可以考虑直接使用document对象的getElementById()方法获取ID元素
</script>
</head>
<body>
<div id="div1">测试盒子</div>
<div id="a.b">div1</div>
<div id="a:b">div2</div>
<div id="[div]">div3</div>
</body>
</html>