You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

_hyperscript元素作用域变量异常:计数器无法共享变量求助

解决_hyperscript元素作用域变量共享问题

你的问题核心在于:直接在按钮的_hyperscript里写:x时,它默认会创建/引用当前按钮自身的元素作用域变量,而非父级div中定义的:x。要实现变量共享,必须明确指定要访问的父元素作用域。

修改后的可运行代码:

<div _="set :x to 10">
  <button _="on click increment parent's :x by 25 then put it into the next <output/>">
    增加
  </button>
  <output></output>
  <button _="on click decrement parent's :x by 25 then put it into the previous <output/>">
    减少
  </button>
</div>

关键细节:

  • 使用parent's :x(或完整写法the parent element's :x)明确指向父div的作用域变量,避免按钮创建独立的:x
  • 若嵌套层级复杂,可通过closest(div)'s :x精准定位目标元素的作用域,适配更多场景

内容的提问来源于stack exchange,提问作者stackedup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 06:48:11