打字稿表格重置()不起作用

时间:2015-05-01 07:45:46

标签: javascript typescript typescript1.4

我正在使用typescript重置表单,但它不起作用或者typescript编译器(1.0.3版本)无法识别reset()函数。 编译器给出错误

plot.ashape3d.2 <- function (x, clear = TRUE, col = c(2, 2, 2), byComponents = FALSE, 
                             indexAlpha = 1, transparency = 1, walpha = FALSE, ...) 
{
  as3d <- x
  triangles <- as3d$triang
  edges <- as3d$edge
  vertex <- as3d$vertex
  x <- as3d$x
  if (class(indexAlpha) == "character") 
    if (indexAlpha == "ALL" | indexAlpha == "all") 
      indexAlpha = 1:length(as3d$alpha)
  if (any(indexAlpha > length(as3d$alpha)) | any(indexAlpha <= 
                                                   0)) {
    if (max(indexAlpha) > length(as3d$alpha)) 
      error = max(indexAlpha)
    else error = min(indexAlpha)
    stop(paste("indexAlpha out of bound : valid range = 1:", 
               length(as3d$alpha), ", problematic value = ", error, 
               sep = ""), call. = TRUE)
  }
  if (clear) {
    rgl.clear()
  }
  if (byComponents) {
    components = components_ashape3d(as3d, indexAlpha)
    if (length(indexAlpha) == 1) 
      components = list(components)
    indexComponents = 0
    for (iAlpha in indexAlpha) {
      if (iAlpha != indexAlpha[1]) 
        rgl.open()
      if (walpha) 
        title3d(main = paste("alpha =", as3d$alpha[iAlpha]))
      cat("Device ", rgl.cur(), " : alpha = ", as3d$alpha[iAlpha], 
          "\n")
      indexComponents = indexComponents + 1
      components[[indexComponents]][components[[indexComponents]] == 
                                      -1] = 0
      colors = c("#000000", sample(rainbow(max(components[[indexComponents]]))))
      tr <- t(triangles[triangles[, 8 + iAlpha] == 2 | 
                          triangles[, 8 + iAlpha] == 3, c("tr1", "tr2", 
                                                          "tr3")])
      if (length(tr) != 0) 
        rgl.triangles(x[tr, 1], x[tr, 2], x[tr, 3], col = colors[1 + 
                                                                   components[[indexComponents]][tr]], alpha = transparency, 
                      ...)
    }
  }
  else {
    for (iAlpha in indexAlpha) {
      if (iAlpha != indexAlpha[1]) 
        rgl.open()
      if (walpha) 
        title3d(main = paste("alpha =", as3d$alpha[iAlpha]))
      cat("Device ", rgl.cur(), " : alpha = ", as3d$alpha[iAlpha], 
          "\n")
      tr <- t(triangles[triangles[, 8 + iAlpha] == 2 | 
                          triangles[, 8 + iAlpha] == 3, c("tr1", "tr2", 
                                                          "tr3")])
      if (length(tr) != 0) 
        rgl.triangles(x[tr, 1], x[tr, 2], x[tr, 3], col = col[1], 
                      , alpha = transparency, ...)
    }
  }
}

alphabunny <- ashape3d(bunny, alpha = c(0.003))
plot.ashape3d.2(alphabunny, col=5, indexAlpha = "all", transparency=1)
bg3d(1)

这是打字稿代码

Build: Interface 'HTMLFormElement' incorrectly extends interface 'HTMLElement'. C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.4\lib.d.ts

当我将上面的代码复制到js文件时,它的工作正常。

它是什么原因?

3 个答案:

答案 0 :(得分:14)

由于函数getElementById返回更通用的类型HTMLElement,您需要手动断言特定版本:

var dirtyFormID = 'something';
var resetForm = <HTMLFormElement>document.getElementById(dirtyFormID);
resetForm.reset();

答案 1 :(得分:1)

带有打字稿。

为避免Google Chrome错误“ resetForm.reset不是函数”,我在HTML代码中指定了按钮元素“ reset”的类型。指定了一个:

type = 'reset'

使我的功能运行良好。

答案 2 :(得分:0)

请注意,document.getElementById会返回HTMLElement(就像这里已经说过的那样)。 您应该将HTMLElement转换为HTMLFormElement

演员应该是这样的:

var resetForm:HTMLFormElement;
resetForm= <HTMLFormElement>document.getElementById('your form id');
if(resetForm)
    resetForm.reset();
相关问题