Sys.UI.DomElement クラス
更新 : 2007 年 11 月
DOM 要素を操作および検査するためのヘルパー API を提供する、静的なメソッドおよびプロパティを定義します。
名前空間 : Sys.UI
継承 : なし
var domElementVar = Sys.UI.DomElement.getElementById(strDomElementID)
コンストラクタ (C# プログラミング ガイド)
名前 |
説明 |
---|---|
Sys.UI.DomElement クラスの新しいインスタンスを初期化します。 |
メンバ
名前 |
説明 |
---|---|
CSS クラスを、まだ DOM 要素の一部でない場合に、DOM 要素に追加します。 |
|
指定した CSS クラスを DOM 要素が保持しているかどうかを示す値を取得します。 |
|
Sys.UI.DomElement クラスの getElementById メソッドへのショートカットを提供します。 |
|
DOM 要素の位置、幅、および高さを表す整数座標の集合を取得します。 |
|
指定した id 属性を持つ DOM 要素を取得します。 |
|
DOM 要素の絶対位置を、オーナー フレームまたはオーナー ウィンドウの左上隅を基準として取得します。 |
|
Sys.UI.DomElement.setVisible メソッドの呼び出しによって非表示になっているときの DOM 要素のレイアウト特性を表す値を返します。 |
|
DOM 要素が現在 Web ページで表示されるかどうかを示す値を取得します。 |
|
CSS クラスを DOM 要素から削除します。 |
|
DOM 要素の位置を設定します。 |
|
Sys.UI.DomElement.setVisible メソッドの呼び出しによって非表示になっているときの DOM 要素のレイアウト特性を設定します。 |
|
DOM 要素を表示または非表示に設定します。 |
|
DOM 要素に含まれる CSS クラスの表示と非表示を切り替えます。 |
解説
DomElement クラスはインスタンス化できません。
使用例
DomElement クラスを使用する方法を次の例に示します。click ハンドラをボタンに割り当てる代わりとして、pageLoad ハンドラを body 要素に割り当てることが可能です。
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Example</title>
<style type="text/css">
.redBackgroundColor {
background-color:Red;
}
.blueBackgroundColor {
background-color:Green;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" GroupingText="Update Panel">
<b>DomElement Methods</b>
<br />
<asp:Button ID="Button1" runat="server" Text="Toggle CssClass" />
<asp:Button ID="Button2" runat="server" Text="Remove CssClass" />
<p></p>
<b>DomElement Properties</b>
<br />
<asp:Label ID="Label1" runat="server" BackColor="Black" ForeColor="White" Text="Label1" Width="102px"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
<script type="text/javascript">
// Add handler using the getElementById method
$addHandler(Sys.UI.DomElement.getElementById("Button1"), "click", toggleCssClassMethod);
// Add handler using the shortcut to the getElementById method
$addHandler($get("Button2"), "click", removeCssClassMethod);
// Add CSS class
Sys.UI.DomElement.addCssClass($get("Button1"), "redBackgroundColor");
Sys.UI.DomElement.addCssClass($get("Button2"), "blueBackgroundColor");
// Method called when Button1 is clicked
function toggleCssClassMethod(eventElement) {
// Toggle CSS class
Sys.UI.DomElement.toggleCssClass(eventElement.target, "redBackgroundColor");
}
// Method called when Button2 is clicked
function removeCssClassMethod(eventElement) {
// Remove CSS class
Sys.UI.DomElement.removeCssClass(eventElement.target, "blueBackgroundColor");
}
// Get the bounds of the element
var elementRef = $get("Label1");
var elementBounds = Sys.UI.DomElement.getBounds(elementRef);
var result = '';
result += "Label1 bounds x = " + elementBounds.x + "<br/>";
result += "Label1 bounds y = " + elementBounds.y + "<br/>";
result += "Label1 bounds width = " + elementBounds.width + "<br/>";
result += "Label1 bounds height = " + elementBounds.height + "<p/>";
// Get the location of the element
var elementLoc = Sys.UI.DomElement.getLocation(elementRef);
result += "Before move - Label1 location (x,y) = (" +
elementLoc.x + "," + elementLoc.y + ")<br/>";
// Move the element
Sys.UI.DomElement.setLocation(elementRef, 100, elementLoc.y);
elementLoc = Sys.UI.DomElement.getLocation(elementRef);
result += "After move - Label1 location (x,y) = (" +
elementLoc.x + "," + elementLoc.y + ")<br/>";
// Prepare the results
$get('Label2').innerHTML = result;
</script>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Example</title>
<style type="text/css">
.redBackgroundColor {
background-color:Red;
}
.blueBackgroundColor {
background-color:Green;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" GroupingText="Update Panel">
<b>DomElement Methods</b>
<br />
<asp:Button ID="Button1" runat="server" Text="Toggle CssClass" />
<asp:Button ID="Button2" runat="server" Text="Remove CssClass" />
<p></p>
<b>DomElement Properties</b>
<br />
<asp:Label ID="Label1" runat="server" BackColor="Black" ForeColor="White" Text="Label1" Width="102px"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
<script type="text/javascript">
// Add handler using the getElementById method
$addHandler(Sys.UI.DomElement.getElementById("Button1"), "click", toggleCssClassMethod);
// Add handler using the shortcut to the getElementById method
$addHandler($get("Button2"), "click", removeCssClassMethod);
// Add CSS class
Sys.UI.DomElement.addCssClass($get("Button1"), "redBackgroundColor");
Sys.UI.DomElement.addCssClass($get("Button2"), "blueBackgroundColor");
// Method called when Button1 is clicked
function toggleCssClassMethod(eventElement) {
// Toggle CSS class
Sys.UI.DomElement.toggleCssClass(eventElement.target, "redBackgroundColor");
}
// Method called when Button2 is clicked
function removeCssClassMethod(eventElement) {
// Remove CSS class
Sys.UI.DomElement.removeCssClass(eventElement.target, "blueBackgroundColor");
}
// Get the bounds of the element
var elementRef = $get("Label1");
var elementBounds = Sys.UI.DomElement.getBounds(elementRef);
var result = '';
result += "Label1 bounds x = " + elementBounds.x + "<br/>";
result += "Label1 bounds y = " + elementBounds.y + "<br/>";
result += "Label1 bounds width = " + elementBounds.width + "<br/>";
result += "Label1 bounds height = " + elementBounds.height + "<p/>";
// Get the location of the element
var elementLoc = Sys.UI.DomElement.getLocation(elementRef);
result += "Before move - Label1 location (x,y) = (" +
elementLoc.x + "," + elementLoc.y + ")<br/>";
// Move the element
Sys.UI.DomElement.setLocation(elementRef, 100, elementLoc.y);
elementLoc = Sys.UI.DomElement.getLocation(elementRef);
result += "After move - Label1 location (x,y) = (" +
elementLoc.x + "," + elementLoc.y + ")<br/>";
// Prepare the results
$get('Label2').innerHTML = result;
</script>