jQuery中$.inArray()方法

定义和用法

$.inArray() 函数用于在数组中查找指定值,并返回它的索引值(如果没有找到,则返回-1)

提示:源数组不会受到影响,过滤结果只反映在返回的结果数组中。

语法

$.inArray( value, array [, fromIndex ] )

参数 描述

value 任意类型 用于查找的值。
array Array类型 指定被查找的数组。
fromIndex 可选。Number类型 指定从数组的指定索引位置开始查找,默认为 0

使用注意

  $(function () {
            var arr = [1, 2, 3, 4, 5];
            var a = 2;
            alert($.inArray(a, arr)); //这段代码返回正常值为1;
            });
 $(function () {
            var arr = [1, 2, 3, 4, 5];
            var a = "2";
            alert($.inArray(a, arr));  //当把a的值改为字符串2返回值就出错了为-1;所以各位在使用这个$.inArray()方法时,一定要确保数据类型是一样的,虽然javascript是一种弱类型语言;
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码改成可调用函数 : /*! * ASP.NET SignalR JavaScript Library 2.4.3 * http://signalr.net/ * * Copyright (c) .NET Foundation. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. * */ /// <reference path="....\SignalR.Client.JS\Scripts\jquery-1.6.4.js" /> /// <reference path="jquery.signalR.js" /> (function ($, window, undefined) { /// <param name="$" type="jQuery" /> "use strict"; if (typeof ($.signalR) !== "function") { throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js."); } var signalR = $.signalR; function makeProxyCallback(hub, callback) { return function () { // Call the client hub method callback.apply(hub, $.makeArray(arguments)); }; } function registerHubProxies(instance, shouldSubscribe) { var key, hub, memberKey, memberValue, subscriptionMethod; for (key in instance) { if (instance.hasOwnProperty(key)) { hub = instance[key]; if (!(hub.hubName)) { // Not a client hub continue; } if (shouldSubscribe) { // We want to subscribe to the hub events subscriptionMethod = hub.on; } else { // We want to unsubscribe from the hub events subscriptionMethod = hub.off; } // Loop through all members on the hub and find client hub functions to subscribe/unsubscribe for (memberKey in hub.client) { if (hub.client.hasOwnProperty(memberKey)) { memberValue = hub.client[memberKey]; if (!$.isFunction(memberValue)) { // Not a client hub function continue; } // Use the actual user-provided callback as the "identity" value for the registration. subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue), memberValue); } } } } } $.hubConnection.prototype.createHubProxies = function () { var proxies = {}; this.starting(function () { // Register the hub proxies as subscribed // (instance, shouldSubscribe) registerHubProxies(proxies, true); this._registerSubscribedHubs(); }).disconnected(function () { // Unsubscribe all hub proxies when we "disconnect". This is to ensure that we do not re-add functional call backs. // (instance, shouldSubscribe) registerHubProxies(proxies, false); }); proxies['realDataHub'] = this.createHubProxy('realDataHub'); proxies['realDataHub'].client = { }; proxies['realDataHub'].server = { send: function (name, message) { return proxies['realDataHub'].invoke.apply(proxies['realDataHub'], $.merge(["Send"], $.makeArray(arguments))); } }; return proxies; }; signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false }); $.extend(signalR, signalR.hub.createHubProxies()); }(window.jQuery, window));
05-27
您可以将上述代码封装成一个可调用函数,例如: ```javascript function connectToHub() { if (typeof ($.signalR) !== "function") { throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js."); } var signalR = $.signalR; var hubConnection = signalR.hubConnection("/signalr", { useDefaultPath: false }); var proxies = {}; function makeProxyCallback(hub, callback) { return function () { callback.apply(hub, $.makeArray(arguments)); }; } function registerHubProxies(instance, shouldSubscribe) { var key, hub, memberKey, memberValue, subscriptionMethod; for (key in instance) { if (instance.hasOwnProperty(key)) { hub = instance[key]; if (!(hub.hubName)) { continue; } if (shouldSubscribe) { subscriptionMethod = hub.on; } else { subscriptionMethod = hub.off; } for (memberKey in hub.client) { if (hub.client.hasOwnProperty(memberKey)) { memberValue = hub.client[memberKey]; if (!$.isFunction(memberValue)) { continue; } subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue), memberValue); } } } } } hubConnection.starting(function () { registerHubProxies(proxies, true); this._registerSubscribedHubs(); }).disconnected(function () { registerHubProxies(proxies, false); }); proxies['realDataHub'] = hubConnection.createHubProxy('realDataHub'); proxies['realDataHub'].client = {}; proxies['realDataHub'].server = { send: function (name, message) { return proxies['realDataHub'].invoke.apply(proxies['realDataHub'], $.merge(["Send"], $.makeArray(arguments))); } }; return proxies; } ``` 然后您可以在需要连接 SignalR 的地方调用该函数,例如: ```javascript var hubProxies = connectToHub(); hubProxies.realDataHub.server.send('John', 'Hello!'); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值