沉冰浮水

沉冰浮水

做最终到的事,成为最终成为的人!
github
bilibili
mastodon
zhihu
douban

【メモ】JavaScriptおよびjQueryの一般的なコード

JavaScript または jQuery でよく使用されるさまざまなコードのまとめ;

クラスの削除、属性の削除:

// クラスの削除、属性の削除
$("input").removeClass("hidden").removeAttr("disabled");

「- -」「- -」「- -」「- -」

jQuery によるセレクトの操作:

// https://www.cnblogs.com/shanyou/archive/2011/07/11/2103422.html
// val()メソッドを使用して直接値を設定できます
$('select').val(2);

「- -」「- -」「- -」「- -」

正規表現のグループ名とコールバック置換について:

function fnReplace(html, post_id) {
  const fnRegxCB = function () {
    // console.log(arguments); // すべての引数を出力しますが、このオブジェクトは配列ではありません
    // 配列に変換
    const arrArgs = Array.prototype.slice.call(arguments);
    // 名前付きグループは最後の項目に配置されますが、pop()メソッドは元の配列から最後の項目を削除して返します
    const data = arrArgs.pop();
    // デバッグ出力
    console.log(arrArgs, data);
    // 文字列を数値に変換
    post_id = parseInt(post_id);
    data.app_id = parseInt(data.app_id);
    // 余り+3、3<=hash<=9
    const hash = ((post_id + data.app_id) % 7) + 3;
    console.log(hash, post_id + data.app_id);
    return `<a href="${data.url}#${hash}-${post_id}" ${data.attrs}>${data.url}#${hash}-${post_id}</a>`;
  };
  return html.replace(
    /<a href="[^"]+" (?<attrs>[^>]+)>(?<url>https:\/\/app.zblogcn.com\/\?id=(?<app_id>\d+))[^<]*<\/a>/,
    fnRegxCB
  );
  // コールバック処理が不要な場合は、以下のようになります↓
  // return html.replace(
  //   /<a href="[^"]+" (?<attrs>[^>]+)>(?<url>https:\/\/app.zblogcn.com\/\?id=(?<id>\d+))[^<]*<\/a>/,
  //   `<a href="$<url>#${post_id}" $<attrs>>$<url>#${post_id}</a>`
  // );
}

「- -」「- -」「- -」「- -」

オブジェクトを URL パラメータに変換:

// オブジェクトをURLパラメータに変換
const queryString = Object.keys(data)
  .map((key) => key + "=" + data[key])
  .join("&");

「- -」「- -」「- -」「- -」

オブジェクトのキーと値の反復処理:

// オブジェクトのキーと値の反復処理
Object.keys(req.headers).forEach(function (key) {
  console.log(key, req.headers[key]);
});

「- -」「- -」「- -」「- -」

画像の盗難防止を解除:

// 画像の盗難防止を解除
(() => {
  function $na(e) {
    return document.querySelectorAll(e);
  }
  $na("a img").forEach((el) => {
    el.setAttribute("referrerPolicy", "no-referrer");
    console.log(el.getAttribute("src"));
  });
})();

「- -」「- -」「- -」「- -」

あまり使用されないが、メモとして:

(()=>{
 for (let index = 0; index <= 13; index++) {
   console.log(index,index % 7,Math.abs(index % 7 - 3));
 }
})();
読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。