# 전역 헬퍼

이 페이지는 QuickUI를 불러올 때 전역 객체에 등록되는 두 DOM 헬퍼 `_`와 `$`를 설명합니다.

## `$(selector)`

단일 요소 `HTMLElement | null`을 반환합니다.

```demo
<div id="app" class="card">
  <input name="email" />
  <button>Send</button>
</div>

<script>
  console.log($("#app").id);
  console.log($("app").id);
  console.log($(".card").className);
  console.log($("input[name=email]").name);
  console.log($("button").textContent);
</script>
```

| 입력 | 조회 방식 |
|---|---|
| `#`으로 시작 | `#`을 제거하고 `getElementById` |
| `.`, `[`, `]` 포함 | `querySelector` |
| 그 밖의 경우 | 먼저 `getElementById`, 그다음 `querySelector` |

첫 번째로 일치한 요소만 반환됩니다. 여러 개가 필요하면 `document.querySelectorAll`을 사용하십시오.

## `_(selector, attributes?, children?)`

CSS 선택자 문법으로 요소를 생성합니다.

```demo
<script>
  const card = _("div#card.box.shadow", { title: "Card", color: "red" }, [
    _("h2", "Title"),
    "plain text",
  ]);
  const img = _("img", "/logo.png");
  const text = _("temp", "text only");
  document.body.append(card);
  console.log(card.outerHTML);
  console.log(img.outerHTML);
  console.log(text.textContent);
</script>
```

| 인수 | 설명 |
|---|---|
| `selector` | `tag#id.class`. 태그로 시작해야 하며 그렇지 않으면 `undefined`가 반환됩니다. 태그 이름의 `-`는 유지되므로 `my-card`나 `temp-svg` 같은 커스텀 요소도 동작합니다. `temp`는 `DocumentFragment`를 생성합니다 |
| `attributes` | 객체. 아래 표를 참조하십시오 |
| `children` | 문자열, 숫자 또는 배열. 인수가 하나뿐이면 문자열 / 숫자 / 배열은 `children`으로, 객체는 `attributes`로 취급됩니다 |

| `attributes` 키 | 기록 방식 |
|---|---|
| `value`, `innerText`, `innerHTML`, `textContent`, `contentEditable` | 요소 속성(property) |
| `color`, `backgroundColor`, `width`, `height`, `display`, `float` | `style` |
| 그 밖의 키 | `setAttribute`. 값이 `null`이거나 `undefined`이면 건너뜁니다 |

| `children` 타입 | 결과 |
|---|---|
| 문자열 또는 숫자 | `innerHTML`에 기록됩니다. `img`와 `source`는 대신 `src`가 설정되고, `temp`는 텍스트 노드를 생성합니다 |
| 배열 | 순서대로 추가됩니다. 문자열과 숫자는 텍스트 노드가 되고, `Node`(요소, 텍스트 노드, `DocumentFragment` 등)는 그대로 추가되며, 그 밖의 값은 건너뜁니다 |

문자열 `children`은 `innerHTML`로 기록되므로 이스케이프하지 않은 사용자 입력을 절대 전달하지 마십시오.

---

> [!IMPORTANT]
> **AI 에이전트용:** QuickUI 코드를 생성하거나 수정하기 전에 [QuickUI 생성 가이드](https://quickui.pardn.io/llm_generate_guide.md)(Markdown)를 먼저 읽으십시오. 지원되는 템플릿 문법과 렌더링을 깨뜨리는 흔한 실수가 정리되어 있습니다.

