$Component を使用した JavaScript からのコンポーネントの参照
どの Visualforce タグにも id 属性が含まれています。他のタグでタグの id 属性を使用することにより、その 2 つのタグをバインドすることができます。たとえば、<apex:outputLabel> タグの for 属性を <apex:inputField> タグの id 属性と併用することができます。<apex:actionFunction>、<apex:actionSupport>、およびその他の活動指向型のコンポーネントの reRender 属性および status 属性もその他のコンポーネントの id 属性の値を使用します。
この ID は、複数の Visualforce コンポーネントのバインドに使用されるだけでなく、ページを表示する際のコンポーネントのドキュメントオブジェクトモデル (DOM) ID の一部の形成にも使用されます。
JavaScript または他の Web 対応言語で Visualforce コンポーネントを参照するには、そのコンポーネントの id 属性の値を指定する必要があります。DOM ID はコンポーネントの id 属性とその要素を含むすべてのコンポーネントの id 属性の組み合わせで構成されます。
コンポーネントアクセスの例
次の例では、<apex:outputPanel> タグの DOM ID を使用します。このページには 2 つのパネルがあります。最初のパネルには DOM イベントをトリガするチェックボックスがあり、2 番目のパネルにはイベントに応じて変わるいくつかのテキストが含まれています。
ページの上部には、<script> HTML タグ内に含まれる JavaScript があります。イベント (input) をトリガした要素および影響を受けるテキストを含む対象のパネルの DOM ID (textid) を引数としてとります。
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page id="thePage">
18
19 <!-- A simple function for changing the font. -->
20
21 <script>
22
23 function changeFont(input, textid) {
24
25 if(input.checked) {
26
27 document.getElementById(textid).style.fontWeight = "bold";
28
29 }
30
31 else {
32
33 document.getElementById(textid).style.fontWeight = "normal";
34
35 }
36
37 }
38
39 </script>
40
41
42
43 <!-- This outputPanel calls the function, passing in the
44
45 checkbox itself, and the DOM ID of the target component. -->
46
47 <apex:outputPanel layout="block">
48
49 <label for="checkbox">Click this box to change text font:</label>
50
51 <input id="checkbox" type="checkbox"
52
53 onclick="changeFont(this,'{!$Component.thePanel}');"/>
54
55 </apex:outputPanel>
56
57
58
59 <!-- This outputPanel is the target, and contains
60
61 text that will be changed. -->
62
63 <apex:outputPanel id="thePanel" layout="block">
64
65 Change my font weight!
66
67 </apex:outputPanel>
68
69</apex:page>{!$Component.thePanel} 式は、<apex:outputPanel id="thePanel"> コンポーネントによって生成される HTML 要素の DOM ID を取得するために使用されます。