【问题标题】:how can i set double attribute to one element?如何将双重属性设置为一个元素?
【发布时间】:2021-04-12 17:40:04
【问题描述】:

嗨,我可以为同一个元素设置 2 个属性吗? 当我专注于一项时,我想运行 2 个功能?如果没有其他选择,有什么想法吗?谢谢。

 listEl.setAttribute('onfocusout', `updateItem(${index}, ${row})`, 'onfocusout', `hideInputBoxOnElement(${row})`);

【问题讨论】:

  • 设置属性只设置一个属性。您将需要使用两次调用(在此示例中,它只会再添加 7 个字符)developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
  • 能否请您详细说明,谢谢。我希望在我离开目标元素后发生两件事。
  • 查看我发布的链接中的第一个示例
  • 所以我创建了一个 var 选择按钮然后 var.setAttribute 并加倍 Attribute 调用..

标签: javascript html


【解决方案1】:

我认为您正在寻找事件侦听器。 onbluronclickonmouseoveronmouseout 等不是属性,而是在事件发生时触发的内联事件侦听器。

//select the myElement somehow, either using the element's ID, class, etc.
myElement.addEventListener('focusout', function() {
  updateItem(index, row);
  hideInputBoxOnElement(row);
});

您可以在一个元素中拥有任意数量的事件侦听器,因此您甚至可以执行以下操作:

//select the myElement somehow, either using the element's ID, class, etc.
myElement.addEventListener('focusout', function() {
  updateItem(index, row);
});

//select the myElement somehow, either using the element's ID, class, etc.
myElement.addEventListener('focusout', function() {
  hideInputBoxOnElement(row);
});

只是一个建议,您可能想要使用blur 事件而不是focusout 事件。你问有什么区别? In web browsers, what's the difference between onblur and onfocusout? 基本上,focusout 会在元素本身或其任何子元素失去焦点时触发,而 blur 只会为元素本身触发。

人们常说不以前缀on- 开头的javascript 事件侦听器与以前缀on- 开头相比被认为是更好的做法,但我个人认为这没有太大区别.

【讨论】:

  • 啊,我明白操作员现在在问什么了。好地方
  • 我更新了元素变量名以免混淆,事件名应该是focusout
  • 感谢我使用 onblur 作为第二个攻击,它成功了感谢 endothermic_dragon
猜你喜欢
  • 1970-01-01
  • 2023-01-18
  • 1970-01-01
  • 1970-01-01
  • 2015-04-27
  • 2012-02-13
  • 1970-01-01
  • 2011-01-01
  • 2016-10-05
相关资源
最近更新 更多