我开始学习JavaScript,这个问题阻止了我.请看这个:
<script>
$(function () {
// Start the connection.
$.connection.hub.start().done(function () {
// How to define some functions here to call from below script?
});
});
</script>
<script type="text/javascript" gwd-events="handlers">
window.gwd = window.gwd || {};
gwd.boardSelectionChanged = function(event) {
var carousel = document.getElementById("mediaCarousel");
var index = carousel.currentIndex;
// how to pass this index to a function above?
};
</script>
在第一个脚本中,我尝试连接到服务器.在第二个脚本中,用户可以选择图像.
我想将索引从第二个脚本传递到第一个脚本中的函数,以将其发送到服务器.
如何在第一个中定义一个函数以及如何调用它?谢谢.
解决方法:
你可以这样做:
<script>
var newFunction = undefined;
$(function () {
// Start the connection.
$.connection.hub.start().done(function () {
// How to define some functions here to call from below script?
newFunction = function(index){
// Your function processing.
}
});
});
</script>
<script type="text/javascript" gwd-events="handlers">
window.gwd = window.gwd || {};
gwd.boardSelectionChanged = function(event) {
var carousel = document.getElementById("mediaCarousel");
var index = carousel.currentIndex;
// how to pass this index to a function above?
if(newFunction) newFunction(index);
};
</script>