【问题标题】:Property 'style' does not exist on type 'ChildNode' Type Script类型“ChildNode”类型脚本上不存在属性“样式”
【发布时间】:2021-11-30 13:17:51
【问题描述】:

我有如下代码。它工作得很好,但 TypeScript 告诉我: 类型“ChildNode”类型脚本上不存在属性“样式”。这个错误

  const allCoins = Array.from(document.querySelectorAll('li')))
  const xs = !useMediaQuery(theme.breakpoints.down('xs'))
  const md = !useMediaQuery(theme.breakpoints.down('sm'))

  allCoins.map((coins) => {
    if (coins.offsetHeight >= 53 && xs && !md) {
      allCoins.map((coin) => {
        coin.childNodes[0].childNodes[2].style.flex = '1 0 100%'
        coin.childNodes[0].childNodes[2].style.marginLeft = '-3px'
      })
    } else {
      allCoins.map((coin) => {
        coin.childNodes[0].childNodes[2].style.flex = 'unset' 
        coin.childNodes[0].childNodes[2].style.marginLeft = '0'
      })
    }
  })

我尝试了我在 Stack and Fit 上看到的所有解决方案,但没有一个适合我。

【问题讨论】:

  • childNodes里面有childNodes吗?你能提供更多信息吗?像硬币对象的结构
  • @SimaAmini 是的。结构如下:ul -> li -> div -> div
  • 好吧,我终于找到了解决方案(coin.childNodes[0].childNodes[2] as HTMLElement)对我有用
  • 您应该发布一个完整的代码示例,发布您的答案,并接受您自己的答案,以便为未来的读者提供完整的参考点

标签: reactjs typescript dom


【解决方案1】:

这个解决方案非常适合我:)

  const allCoins: HTMLElement[] = Array.from(document.querySelectorAll('li'))
  const xs = !useMediaQuery(theme.breakpoints.down('xs'))
  const md = !useMediaQuery(theme.breakpoints.down('sm'))

  allCoins.map((coins) => {
    if (coins.offsetHeight >= 53 && xs && !md) {
      allCoins.map((coin) => {
        (coin.childNodes[0].childNodes[2] as HTMLElement).style.flex = '1 0 100%';
        (coin.childNodes[0].childNodes[2] as HTMLElement).style.marginLeft = '-3px';
      })
    } else {
      allCoins.map((coin) => {
        (coin.childNodes[0].childNodes[2] as HTMLElement).style.flex = 'unset';
        (coin.childNodes[0].childNodes[2] as HTMLElement).style.marginLeft = '0';
      })
    }
  })

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2018-06-08
  • 2021-01-09
  • 1970-01-01
  • 2021-07-10
  • 2021-06-18
  • 2017-08-15
  • 2019-09-29
相关资源
最近更新 更多