我的印象是,我可以通过使用bind
将参数传递给一个函数,然后调用这些参数,就好像它们已经正常传递一样。为什么我需要这样做:为什么bind看起来像数组一样呈现参数?
$(document).ready(function() {
var func = function(button) {
button[0].on('click', function() {
alert('Hello World.');
})
}
func.bind(null, [$('.button')])();
});
当好像我应该能够只是这样做:
$(document).ready(function() {
var func = function(button) {
button.on('click', function() {
alert('Hello World.');
})
}
func.bind(null, [$('.button')])();
});
例,治疗button
参数作为一个数组:
$(document).ready(function() {
var func = function(button) {
button[0].on('click', function() {
alert('Hello World.');
})
}
func.bind(null, [$('.button')])();
});
html, body, div {
padding:0;
margin:0;
display:flex;
justify-content:center;
}
.button {
background-color:gray;
margin:50px;
padding:50px;
width:50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="button">Button</div>
它将它视为一个数组,因为你绑定了一个数组。 –