jquery的文档说明closest如下:
.closest(selector [,context])
...
context
类型:Element
可以在其中找到匹配元素的DOM元素.如果没有传递上下文,那么将使用jQuery集的上下文.
据我了解,粗体文本意味着这两个陈述应该是等价的:
set.closest("a");
set.closest("a", set.context);
Run Code Online (Sandbox Code Playgroud)
哪些set是jquery集.
但是,情况似乎并非如此:
var context = $("#inner")[0];
var set = $("#el", context);
// the set's context is correctly the "inner" element
set.text("context: " + set.context.id);
// if the set's context is used, this closest should match nothing, but it matches and sets the color
set.closest("#outer").css("color", "red");
// with the context explicitly set, the "outer" is not found and no background color is set
set.closest("#outer", …Run Code Online (Sandbox Code Playgroud)我正在关注本教程[chart.js][1],我得到了这个错误和这一行
var ctx = $("#myChart").get(0).getContext("2d");
Run Code Online (Sandbox Code Playgroud)
在哪里$("#myChart")代表<figure id="myChart" class="chart"></figure>
抛出此错误
Uncaught TypeError: Object #<HTMLElement> has no method 'getContext'
Run Code Online (Sandbox Code Playgroud)