1

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

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

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

当单击元素时,行为只是在元素周围放置一个粗边框 - 但是 - 他们必须将先前选择的元素设置为正常边框。

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

<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 组件。

你会建议我怎么做?

先感谢您。

4

1 回答 1

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>
于 2009-10-22T15:32:30.850 回答