什么是获得所有以任何类开头的div的最佳方法input?换句话说,a并且b应该从什么下方,但不退还c.
<div id="a" class="input1 foo"></div>
<div id="b" class="foo input2"></div>
<div id="c" class="xinput3 foo"></div>
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,在这里接受的表面方式是做$("div[class^='input']");但当然是错过了b.当然$("div[class*='input']");会给出误报c.
我能想到的最好的就是这种怪异
function getAllInputDivs() {
return $("div").filter(function (i, currentDiv) {
return $.grep($(currentDiv).attr("class").split(" "), function (val) {
return val.substr(0, "input".length) === "input";
}).length > 0;
});
}
Run Code Online (Sandbox Code Playgroud)
有更干净的方式吗?这是上面的工作小提琴