IE VBScript HTC行为 - 所有行为实例之间的静态变量?

时间:2009-10-22 00:18:11

标签: vbscript behavior html-components

我不是100%肯定我应该问什么问题 - 因为我非常确定最好的方法来做到这一点..所以让我描述一下我正在尝试做什么(使用简化的例子)我们我会从那里出发。

你有任意HTML元素(IMG,A,TD,等等)。 通过CSS,他们被分配了一个HTML行为

.BoldSelection { 
    behavior: url(SelectBold.htc); 
    border: thin solid black;  
}

行为只是在单击元素时在元素周围放置一个粗边框 - 但是 - 它们必须使用普通边框设置先前选定的元素。

所以这是HTC源码。如果CurrentlyFocusedElementID在行为的所有实例之间是静态的,那么这将起作用。但事实并非如此。

<Public:Attach Event="onContentReady" onEvent="LoadInit" />

    <Script Language="VBScript" type="Text/VBScript">

        Sub LoadInit
            element.onClick = getRef("setFocusedElement")
        End Sub

        Sub setFocusedElement
            set ele = document.getElementByID(CurrentlyFocusedElementID)
            ele.style.border = "thin solid black"
            CurrentlyFocusedElementID = element.id
            element.style.border = "thick solid black"
        End Sub

    </Script>

我还认为,如果我可以在包含文档的DOM中存储任意属性或属性,那么我可以将其用作查找最后一个活动元素的常用位置...唉我无法想办法不使用某种黑客(即劫持身体的类值)来做到这一点

我想将代码全部包含在HTC中。我喜欢这种方式的模块化方式..这样我可以简单地分配CSS行为及其完成 - 没有回调..没有父属性..没有要声明的HTML组件。

你怎么建议我这样做?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

这个拼图的缺失部分是...... expandos。自定义任意属性。这是完成的.HTC

<Public:Attach Event="onContentReady" onEvent="LoadInit" />

  <Script Language="VBScript" type="Text/VBScript">

    ' This is an example HTC only.   If we were only setting borders, it'd make more sense to store
    ' the previous element's border type and keep the rest of the formatting.  For simplicity we are
    ' swapping out the class name

    Sub LoadInit

      ' No ID defined for this element.  Highlight it for the developer
      If element.id = "" Then
        element.style.bordercolor = "rgb(200,50,10)"
        element.style.borderwidth = "thin"
        element.style.borderstyle = "dashed"
        Exit Sub
      End If

      ' Attach our Click Events
      element.onClick = getRef("BoldIt")
      element.onDblClick = getRef("BoldItMore")

    End Sub


    ' Changes the Class Name for the current element, and if a previously
    ' selected element exists, restore its original classname
    Sub changeClass(newCSSClass)
      ' Storing the Expando on the document.body element
      Set ele = window.document.body

      ' Retrieve our two custom attributes - the ID of the element, and what its original ClassName was.
      LastEle = ele.getAttribute("LastHighlightedEle")
      LastEleClass = ele.getAttribute("LastHighlightedEleClass")

      ' If there was in fact a previously selected element - restore the classname
      If not isnull(LastEle) then
        set oldEle = window.document.getElementByID(LastEle)
        oldEle.className = LastEleClass
        set oldEle =  Nothing
      End If

      ' Set our two custom attributes to this element and adjust this element's CSS ClassName
      LastEle = element.id
      LastEleClass = element.className
      ele.setAttribute "LastHighlightedEle",LastEle
      ele.setAttribute "LastHighlightedEleClass",LastEleClass
      element.className = newCSSClass
    End Sub

    ' Single Click Event - 'Thick' is a CSS Style for a 3px border
    Sub BoldIt
      changeClass("Thick")
    End Sub

    ' Double Click Event - 'Thicker' is a CSS Style for a 4px border
    Sub BoldItMore
      changeClass("Thicker")
    End Sub

  </Script>