6

jQueryまたはJavascriptに、何かを除いて、または何かだけ除いてボタンの押下を処理するためのショートカット(実際には関数)はありますか?例:

$(input).keypress('nonfunctional' function(){ 
  // do something
});

これは、[az] [0-9]ボタンが押されたときにのみトリガーされ、単一のボタンを無視しますshiftctrl、処理shift+a=> Aが押されましたか?

PSiはそのことを知っていif(key.code == 123)ます...

4

6 に答える 6

10

いいえ、特定のキーを除外する場合は、event.keyCode / event.whichプロパティがあります。

于 2013-01-22T13:40:54.093 に答える
2

または、jqueryのkeypressを拡張できます。私が推測するこのようなもの:

$.fn.keypressBut = function(codes, callback) {
    $(this).keypress(function(e) {
        ~$.inArray(e.keyCode, codes) || callback.call(this, e);
    });
}

// Lets ignore Enter and Space
$('input').keypressBut([13, 32], function(e) {
    alert(e.keyCode);
})
于 2013-01-22T13:53:12.787 に答える
2

次のようなことができます

  1. fn次のようなパラメーターを取る関数でjquerysプロパティを拡張します

    • 一部のデータ
    • コールバック関数
  2. 検証関数を書く

    • keyCode を文字列に変換します
    • 正規表現と照合します。
    • shiftキーが押された場合
      • 大文字に変換する
    • Ctrl/Alt キーが押されたなど、他の条件が満たされているかどうかを確認します。
    • 結果を返します。
  3. 検証が成功した場合
    • コールバック関数を実行する

コードサイトでは、これは好きかもしれません

  $.fn.selectedKey = function (cb, data) {
      def.call(data, {
          ctrlKey: 2, //0: musn't be pressed, 1: must be pressed, 2: both.
          altKey: 2, // "
          invert: 0, //inverts the filter
          filter: /.*/, // A Regular Expression, or a String with a Regular Expression
          preventDefault: false //Set to true to prevent Default.
      }); //Sets the default Data for the values used,

      function validate(e) {
          var key = e.char = String.fromCharCode(e.keyCode || e.which); // Converts the pressed key to a String
          if (e.shiftKey) key = key.toUpperCase(); //Handles Case Sensitivity.
          var exp = new RegExp(e.data.filter.replace(/\\\\(\d)/g, String.fromCharCode("$1"))); //Creates a new RegExp from a String to e.g. allow "\2" to match the keyCode 2
          var c = !! (e.data.ctrlKey ^ e.ctrlKey ^ 1 > 0); //c == true if the above stated conditions are met e.g Ctrl Key Pressed and `ctrlKey == 1` -> true
          var a = !! (e.data.altKey ^ e.altKey ^ 1 > 0); //e.g Alt Key Pressed and `altKey == 0` -> false
          return (exp.test(key) && (c && a)); //Returns the validation Result
      }

      function def(obj) { //a minimal helper for default values
          for (var prop in obj) {
              this[prop] = this[prop] || obj[prop];
          }
      }
      this.keypress(data, function (e) {
          if (e.data.preventDefault) e.preventDefault();
          if (validate(e) != e.data.invert) cb(e); //Calls the callback function if the conditions are met
      });
  };

次に、次の方法を使用できます

正規表現で

  $("body").selectedKey(function (e) {
      console.log("All lower characters Numbers and 'A': " + e.char);
  }, {
      filter: /[a-z]|[0-9]|A/,
      ctrlKey: 2,
      altKey: 2
  });

これは、ctrl と alt の状態に関係なく、[az] または [0-9] または Shift キー + A が押された場合にトリガーされます。

またはキーコード

  $("body").selectedKey(function (e) {
      // do somet
      console.log("KeyCode 2 " + e.char);
  }, {
      filter: "\\2", //Ctrl + b
      ctrlKey: 1,
      altKey: 2
  });

ctrl + b を押すとトリガーされます

これらの両方を組み合わせることもできます。

Heres on JSBinの例をいじってみましょう。

于 2013-01-22T14:24:51.450 に答える
1

押されたキーコードを検出するために大きなifステートメントを書きたくない場合は、キープレスをフィルタリングするためにクラスと正規表現を使用するためのjQueryプラグインがあります。これはkeyfilterと呼ばれます。例は

$(selector).keyfilter(/[a-z]/);
于 2013-01-22T13:45:22.930 に答える
1

たとえば、以下のこの関数は数字のみを許可し、keydown イベントを使用してこの関数を処理します。

function OnlyNumbers(event) {
    if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || (event.keyCode == 65 && event.ctrlKey === true) || (event.keyCode >= 35 && event.keyCode <= 39)) { return; }
    else { if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) { event.preventDefault(); } }
}
于 2013-01-22T13:45:52.570 に答える
0

event.altKey/event.shiftKey/event.ctrlKey を試しましたか??

于 2013-01-22T13:46:44.690 に答える