实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>1.jQuyerr的工作原理</title> <style type="text/css"> ul { margin: 30px; padding:10px; overflow: hidden; } li { list-style-type: none; width: 40px; height: 40px; margin-left: 10px; background-color: lightskyblue; text-align: center; line-height: 40px; font-size: 1.2em; font-weight: bolder; float: left; border-radius: 50px; box-shadow: 2px 2px 2px #808080; } /*将对一个小球背景色换成绿色*/ /*li:first-child { background-color: lightgreen; } li:nth-child(4) { background-color: orangered; color: white; }*/ /*li:nth-child(4) ~ * { background-color: lightgreen; }*/ </style> </head> <body> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> </ul> </body> </html> <script type="text/javascript"> // document.getElementsByTagName('li')[0] // li.style.backgroundColor = 'lightgreen' // querySelector() // 只会返回满足条件的一个元素,但仅返回第一个 // var li = document.querySelector('li:first-child') // li.style.backgroundColor = 'lightgreen' // var li = document.querySelector('li:nth-child') // li.style.backgroundColor = 'orangered' // li.style.color = 'white' // var li = document.querySelector('li:nth-child(4) ~ *') // li.style.backgroundColor = 'lightgreen' //querySelectorAll () // var li = document.querySelectorAll('li:nth-child(4) ~ *') // alert(li.length) // for (var i=0;i<li.length;i++){ // li[i].style.backgroundColor = 'lightgreen' // } </script> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $('li:nth-child(4)~*').css('background-color','lightgreen') </script>
运行实例 »
点击 "运行实例" 按钮查看在线实例