你是正確的,它不是很難使用。這只是一個用普通數組獲得創意的問題。下面是我所做的:
<%
Function Img(aParamArray)
Dim oImageTag,aImageTagKeys, val, param, key, output
Set oImageTag = CreateObject("Scripting.Dictionary")
For Each param In aParamArray
val = Split(param, "::")
If Ubound(val) = 1 Then
oImageTag(val(0)) = val(1)
End If
Next
aImageTagKeys = oImageTag.Keys
Img = "<img "
For Each key in aImageTagKeys
If oImageTag(key) <> "" Then
Img = Img & key & "=""" & oImageTag(key) & """ "
End If
Next
If iDocType = 0 Or iDocType = 1 Or iDocType = 6 Then
Img = Img & ">"
Else
Img = Img & "/>"
End If
End Function
%>
--OR我可以設置支持attributes--
<%
Function Img(aParamArray)
Dim oImageTag,aImageTagKeys, val, param, key, output
Set oImageTag = CreateObject("Scripting.Dictionary")
oImageTag("src") = ""
oImageTag("alt") = ""
oImageTag("class") = ""
oImageTag("id") = ""
oImageTag("width") = ""
oImageTag("height") = ""
oImageTag("usemap") = ""
oImageTag("title") = ""
oImageTag("style") = ""
oImageTag("dir") = ""
oImageTag("lang") = ""
oImageTag("ismap") = ""
oImageTag("onabort") = ""
oImageTag("onclick") = ""
oImageTag("ondblclick") = ""
oImageTag("onmousedown") = ""
oImageTag("onmouseout") = ""
oImageTag("onmouseover") = ""
oImageTag("onmouseup") = ""
oImageTag("onkeydown") = ""
oImageTag("onkeypress") = ""
oImageTag("onkeyup") = ""
For Each param In aParamArray
val = Split(param, "::")
If Ubound(val) = 1 Then
If oImageTag.Exists(val(0)) Then
oImageTag(val(0)) = val(1)
End If
End If
Next
aImageTagKeys = oImageTag.Keys
Img = "<img "
For Each key in aImageTagKeys
If oImageTag(key) <> "" Then
Img = Img & key & "=""" & oImageTag(key) & """ "
End If
Next
If iDocType = 0 Or iDocType = 1 Or iDocType = 6 Then
Img = Img & ">"
Else
Img = Img & "/>"
End If
End Function
%>
默認值,僅輸出,並調用它是這樣的:
<% =Img(Array(_
"src::http://www.domain.com/img.jpg",_
"alt::Some alt text",_
"width::30",_
"height::30",_
"class::noborder"_
)) %>
現在,我可以輕鬆地控制圖像標籤的輸出,無論doctype如何,並且從SQL服務器輸出圖像將更加容易,因爲我可以將僞關聯數組從正常索引的數組中取出。
要想清楚這一點不是爲了製作圖像標籤,而是實際上是基於來自SQL服務器的多個因素和數據構建數據和表單的整個視圖,但我需要簡化它以找出它。現在它工作完美。
感謝您的建議!
嗨,謝謝。我已經查看了Dictionary對象,我可以看到將它用作關聯數組的唯一方法是創建一個命名對象,並逐個添加我的參數作爲值。這不是我想要做的,因爲我試圖避免一個命名變量,並希望能夠將一個匿名數組作爲參數。 – Dexter 2010-11-16 14:31:17
你能給我一個簡單的例子,上面的調用移植到VBScript使用Dictionary對象,所以我可以看到,如果我們看到它的方式相同?謝謝 – Dexter 2010-11-16 14:32:24