zepto 基础知识(2)

时间:2023-03-10 07:04:37
zepto 基础知识(2)

20.append
  append(content) 类型:self
  在每个匹配的元素末尾插入内容(内部插入)。内容可以为html 字符串。dom节点,或者节点组成的数组。
    $('ul').append('<li>new list item</li>')

21.appendTo
  appendTo(target) 类型:self
  将匹配的元素插入到目标元素的末尾(内部插入)
    $('<li>new list item</li>').appendTo('ul')

22.attr
  attr(neme) 类型:string
  attr(name,value) 类型: self
  attr(name,function(index,oldValue){...}) 类型 : self
  attr({name:value,name2:value2,....}) 类型:self
  读取或设置dom的属性,如果没有给定value参数,则读取对象集合中第一个元素的属性值,当给定了v
    lue 参数。则设置对象集合中所有元素的属性值,当vale 参数为null 。
    var form = $('form')
    form.attr('action')
      //=> 读取值
    form.attr('action', '/create')
      //=> 设置值
    form.attr('action', null)
      //=> 移除属性

      // 多个属性:
    form.attr({
      action: '/create',
      method: 'post'
    })

23.before
  before(content) 类型:self
  在匹配每个元素的前面插入内容(外部插入)内容可以为html字符串,dom节点,或者节点组成的数组。
    $('table').before('<p>1234</p>')

24.children
  children([selector]) 类型:collection
  获得每个匹配元素集合元素的直接子元素,如果给定selector 那么返回的结果中只包含css选择器元素。
    $('ol').children('*:nth-child(2n)')

25.clone
  clone() 类型:collection
  通过深度克隆来复制集合中的所有元素。

26.closest
  closest(selector,[context]) 类型: collection
  closest(collection)
  closest(element)
  从元素本身开始,逐级向上级元素匹配,并返回最先匹配selector的元素,如果给定context
  节点参数,那么只匹配该节点的后代元素,这个方法域parebts(selector)有点像素,但它只返回最先匹配的祖先元素。
  如果参数是一个Zepto对象集合或者一个元素,结果必须匹配给定的元素而不是选择器。
  var input = $('input[type=text]')
  input.closest('form')

27.concat
  concat(nodes,[node2....]) 类型:self
  添加元素到一个Zepto 对象集合形成一个新数组,如果参数是一个数组,那么这个数组中的元素
  将会合并到Zepto对象集合中。
  这是一个Zepto 提供的方法。

28.contents
  contents() 类型collection
  获得每个匹配元素集合元素的子元素,包括文字和注释节点。

29.css
  css(property) 类型:value
  css([property1,property2,...]) 类型:object
  css(property,value) 类型: self
  css([property:value,property2:value2,....]) 类型: self
  读取或设置DOM元素的css属性。当value 参数不再的的时候,返回对象集合中第一个元素的css
  属性,当value 参数存在时,设置对象集合中每一个元素的对应css 属性。
  当value 为空,那个css 属性将会被移除,当value参数为一个无单位的数字,如果该css属性需要单位
  "px" 将会自动添加到该属性上。
    var elem = $('h1')
    elem.css('background-color')
      // read property
    elem.css('background-color', '#369')
      // set property
    elem.css('background-color', '')
      // remove property
    elem.css({ backgroundColor: '#8EE', fontSize: 28 })
      // set multiple properties:
    elem.css(['backgroundColor', 'fontSize'])['fontSize']
      // read multiple properties:

30.data
  data(name) 类型:value
  data(name,value) 类型:self
  读取或写入dom的 data-* 属性。行为有点像attr, 但是属性名称前面加上data-
  当读取属性值时,会有下列转换
  “true”, “false”, and “null” 被转换为相应的类型;
  数字值转换为实际的数字类型;
  JSON值将会被解析,如果它是有效的JSON;
  其它的一切作为字符串返回。
  Zepto 基本实现`data()`只能存储字符串。如果你要存储任意对象,请引入可选的“data”模块到你构建的Zepto中。

31.each
  each(function(index,item){...}) 类型:self
  遍历一个对象集合每一个元素,在迭代函数中,this关键字指向当前(作为函数的第二个参数传递)
  如果迭代函数返回false ,遍历结束。
  $('form input').each(function(index){
    console.log('input %d is %o',index, this)
  })

32.empty
  empty() 类型:self
  清空对象集合中没个元素的DOM 内容。

33.eq
  eq(index) 类型:collection
  从当前对象集合中获取给定索引(以0为基数)的元素。
    $('li').eq(0)
    $('li').eq(-1)

34.filter
  filter(selector) 类型: collection
  filter(function(index){...}) 类型:collection
  过滤对象集合,返回对象结婚中满足css选择器的项,如果参数作为一个函数,函数返回有实际值的时候,元素才会被返回,在函数中,this 关键字指向当前的元素。

35.find
  find(selector) 类型:collection
  find(collection) 类型:collection
  find(element) 类型:collection
  在当前对象集合内查找符合css选择器的每个元素的后代元素。
  如果给定Zepto对象集合或者元素,过滤他们,只有当他们在当前Zepto 集合对象中时,才会被返回。
    var form = $('#myform')
    form.find('input,select')

36.first
  first() 类型:collection
  获取当前对象集合中的第一个元素。
    $('form').first()

37.forEach
  forEach(function(item,index,array){...},[context])
  遍历对象集合中每个元素,有点类型each,但是遍历函数的参数不一样,当函数返回false的时候,遍历不会停止。
  这是一个Zepto提供的方法,不是jquery的API。

38.get
  get() 类型:array
  get(index) 类型 DOM node
  从当前对象集合中获取所有元素或单个元素。当index参数不存在时,一普通数组的方式返回所有的元素,当指定index时,只返回改置的元素,这点与eq不同,该方法返

  回 的是DOM节点,不是Zepto对象。
  var elements = $('h2')
  elements.get()
    //=> get all headings as an array
  elements.get(0)
    //=> get first heading node

39.has
  has(selector) 类型:collection
  has(node) 类型 :collection
  判断当前对象集合的子元素是否有符合选择器的元素,或者是否包含指定的DOM节点,如果有
  则返回新的对象集合,该对象过滤掉不含有选择器匹配元素或者不含有指定DOM节点的对象。
    $('ol > li').has('a[href]')

40.hasClass
  hasClass(name) 类型:boolean
  检查对象集合中是否有元素含有指定的class.
  <ul>
    <li>list item 1</li>
    <li class="yaotaiyang">list item 2</li>
    <li>list item 3</li>
  </ul>
  <p>a paragraph</p>
  $("li").hasClass("yaotaiyang") //true