Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div>我是div1</div>
  <div>我是div2</div>
  <div>我是div3</div>
  <div>我是div4</div>
  <div>我是div5</div>
  <div>我是div6</div>
</body>
</html>
 
window.jQuery = function(nodeOrSelector) {
  var nodes = {}
  // 检测传递的是元素还是一个选择器
  if (typeof nodeOrSelector === 'string') {
    var temps = document.querySelectorAll(nodeOrSelector)
    for (let i = 0; i < temps.length; i++) {
      nodes[i] = temps[i]
    }
    nodes.length = temps.length
  } else if(nodeOrSelector instanceof Node) {
    nodes = {
      0: nodeOrSelector,
      length: 1
    }
  }
  nodes.addClass = function(className) {
    for (let i = 0; i < nodes.length; i++) {
      nodes[i].classList.add(className)
    }
  };
  nodes.getText = function(){
    var texts = []
    for (let i = 0; i < nodes.length; i++) {
      texts.push(nodes[i].textContent)
    }
    return texts.join('')
  }
  
  nodes.setText = function(text){
    for (let i = 0; i < nodes.length; i++) {
      nodes[i].textContent = text
    }
  }
  
  return nodes;
}
window.$ = jQuery
var $div = $('div')
//console.log($div)
var temp = $div.addClass('red')
alert($div.getText())
  //.setText('测试').addClass('blue')
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers