FLX*_*FLX 2 javascript jquery jquery-plugins
我有这个自定义的jQuery函数:
jQuery.fn.extend({
disable: function () {
return $(this).each(function () {
// function code
});
}
});
Run Code Online (Sandbox Code Playgroud)
当我做这样的事情时:
container.find('input')
.disable()
.end()
.hide();
Run Code Online (Sandbox Code Playgroud)
容器没有隐藏,因为在结束后我没有检索容器.如果我用像prop()或的核心函数替换disable css(),那么end()得到容器.
有没有办法让扩展函数像普通函数一样?
不要$(this)在disable()自定义功能里面使用,请使用this.
jQuery.fn.disable = function() {
return this.each(function() {
console.log('disable');
});
};
$('#container').find('input')
.disable()
.end()
.css('background', 'yellow');Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div id="container">
<input name="test" />
</div>Run Code Online (Sandbox Code Playgroud)
或者,您可以通过hide()这种方式更改顺序,这是您不需要的end().
container
.hide()
.find('input')
.disable();
Run Code Online (Sandbox Code Playgroud)