v2.0.3

String 擴充

最後更新


RenderJS 為 String.prototype 加入的方法與 getter:以標籤選擇器字串建構 DOM、元素查詢、JSON/HTML 工具與 URL 捷徑。

String


原始碼:src/prototype/String.ts
方法/屬性說明
"selector".RJS(body)於符合選擇器的元素上啟動 RJS 實例;見 核心概念
"tag.class#id"._(attrs, children)依標籤選擇器字串建立真實 DOM 元素(含 SVG 命名空間偵測、input/textarea type/placeholder 解析、lazyload 屬性接線);.class 片段在 class 列表中保留開頭的點("p.note" 產生 class=".note")
"selector".$取得符合的單一元素:開頭為 # 時以其餘字串走 getElementById,含 .、[、] 時走 querySelector,其餘先試 getElementById 再 fallback 到 querySelector
"selector".$all取得所有符合元素的陣列([...querySelectorAll(...)])
"icon-a icon-b"._faGetter,把第一個空格換成 . 後建構 "i.icon-a.icon-b"._(),產生 Font Awesome <i> 元素;與 _ 相同,class 保留開頭的點(<i class=".icon-a .icon-b">)
"str".$json安全的 JSON.parse(this);解析失敗時回傳 undefined(並記錄 log)
"str".$$json$json 回傳值不是 null/undefined 時為 true("null".$$json 為 false)
"str".$htmlHTML 逸出 、<、>、"、'
"str".copy()非同步;透過 navigator.clipboard.writeText 寫入剪貼簿
"str".$regexp(flags?, force?)依字串建構 RegExp;force: true 會先逸出正則特殊字元
"str".$base64(mimeType)將 data URL 字串第一個 , 之後的 base64 內容解碼為 Blob
"str".$$(filter?)正則測試、字串完全相等,或(無參數時)去除空白後非空的判斷
"url".$urlnew URL(this),相對路徑時 fallback 為 new URL(location.origin + this)
"url".$queryAll解析後的 query string 物件(透過 $url.$queryAll)
"url".$$200(isImg) / $req(body, once) / _history(title) / _query(obj) / __history(title) / __query(obj) / query_(keys) / query__()轉發至下方 URL 對應的方法,套用於 this.$url
"url".$img() / "url".$query()有註冊轉發,但 URL.prototype 沒有 $img 或 $query(對應的是 $image 與 $$query),呼叫時拋出 TypeError

範例


建立元素後,印出查詢、JSON、HTML 跳脫、查詢字串與 regex 輔助函式的結果。
  1. <div id="box"></div>
  2. <script>
  3. "box".$._child("p#note"._("built from a tag-selector string"));
  4. console.log("box".$.innerHTML);
  5. console.log("p.note"._().className);
  6. console.log('{"a":1}'.$json, '{"a":1}'.$$json);
  7. console.log("<b>&</b>".$html);
  8. console.log("https://renderjs.pardn.io/docs?page=2&lang=en".$queryAll);
  9. console.log("a.b".$regexp("g", true).source);
  10. </script>
即時預覽 · @pardnchiu/renderjs@2.0.3
EN