jQuery

更新日期: 2015-12-12 阅读次数: 6065 分类: Javascript

What is the difference between jQuery's space and > selectors?

  • > 是所有的直系子女中符合条件的
  • 空格是所有后辈中符合条件的

所以,从逻辑严谨的角度看,能用 > 的时候,尽量用 >

参考:

  • http://stackoverflow.com/questions/1218068/what-is-the-difference-between-jquerys-space-and-selectors

text() 与 val() 的区别

  • val() 是获取 input 的 value 值
  • text() 则是获取标签里的文本(div, td 等)

参考:

  • http://stackoverflow.com/questions/807867/what-is-the-difference-between-jquerys-functions-val-and-text

get(index) 与 eq(index) 的区别

  • The .get() method returns a DOM element at the given index.

  • The .eq() method returns a DOM element at the given index, wrapped in a jQuery object. In other words,

    $("selector").get(0)

is equivalent to

$("selector").eq(0).get(0)

实际问题

var team_score = $(">td", tr_team_score).get(8).text()

上面一行会报错误如下

<HTMLTableCellElement> has no method 'text'

如果将 get 改为 eq,则运行正确. 原因就是,text() 方法是 jQuery object 才有的。

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式