2009-11-11 56 views

回答

6

您可以向對象添加屬性。

例如

Image img = new Image(); 
img.Attributes.Add("onmouseover", "myjavascriptfunction();"); 

要設置基於所述對象的id的paramater,使用該:

Image img = new Image(); 
img.Attributes.Add("onmouseover", "myjavascriptfunction(" + img.ClientID + ");"); 
+0

我必須在函數中添加image-id的參數,所以「function(img123)」,但由於runat = Server的ID在運行時發生變化,該怎麼辦? – Kovu 2009-11-11 08:56:42

+0

編輯添加你需要的東西。 – cjk 2009-11-11 09:16:01

+0

完美地工作 – Kovu 2009-11-11 09:46:07

0

在解決方案將是使用一個CSS類和jQuery:

<img id="generatedId" class="myHoverImage" /> 

的javascript:

$("img.myHoverImage").mouseover (function() { 

    // you can access the generated id:  

    alert (this.id); 

    // --> your code goes here <-- \\  

}); 
1

使用ck的例子,你可以請使用服務器控件上的ClientID屬性來嘗試執行的操作。像這樣:

yourImage.Attributes.Add("onmouseover", "jsfunction(" + yourImage.ClientID + ");");