【问题标题】:how to assign same value to two variables in one line如何在一行中为两个变量分配相同的值
【发布时间】:2019-11-19 07:54:51
【问题描述】:

document.getElementsByTagName('span')[0].onclick = function(ev) {
  this.innerHTML, this.parentNode.id = "Z";
 
  //^^^^^^^^^^^^^^^^^^^^^
  //this.innerHTML = "Z";
  //this.parentNode.id = "Z";
}
#Z {
  color: red;
}
<div>
  <span>x</span>
</div>

我想将值 "Z" 应用到 this.innerHTML, this.parentNode.id 单行而不是两行,但它没有使用逗号工作

【问题讨论】:

  • 不要压缩你的开发代码,让压缩工具为生产代码做。

标签: javascript


【解决方案1】:

只需链接作业。

也许值得一看:In JavaScript, is chained assignment okay?

comma operator 有不同的目的和结果。

document.getElementsByTagName('span')[0].onclick = function(ev) {
    this.innerHTML = this.parentNode.id = "Z";
}
#Z { color: red; }
&lt;div&gt;&lt;span&gt;x&lt;/span&gt;&lt;/div&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2017-09-16
    • 2022-06-11
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    相关资源
    最近更新 更多