沙箱小案例
substr截取, 从指定的字段开始截取
(function () { var str="小白喜欢小黑"; str=str.substr(2); console.log(str); }());
substr截取, 从指定的字段开始截取
//沙箱 (function () { var str="小明喜欢小红"; str=str.substr(2); console.log(str); }());
沙箱的案例,操作页面元素
<div>这是div</div> <div>这是div</div> <div>这是div</div> <p>这是p</p> <p>这是p</p> <p>这是p</p> <script> var getTag = 10; var dvObjs = 20; var pObjs = 30; (function () { //根据标签名字获取元素 function getTag(tagName) { return document.getElementsByTagName(tagName); } //获取所有div var dvObjs = getTag("div"); for (var i = 0; i < dvObjs.length; i++) { dvObjs[i].style.border = "2px solid pink"; } //获取所有p var pObjs = getTag("p"); for (var i = 0; i < pObjs.length; i++) { pObjs[i].style.border = "2px solid pink"; } }()); console.log(getTag); console.log(dvObjs); console.log(pObjs); </script>