【问题标题】:Is it possible to capture the attribute of another element with an onclick event handler?是否可以使用 onclick 事件处理程序捕获另一个元素的属性?
【发布时间】:2018-04-05 14:13:12
【问题描述】:

这个问题不言自明。

而不是像这样从自身捕获:this.getAttribute('id'))this.id;是否可以从另一个标签中捕获属性?

编辑:

当然,点击一下:

假设:

<button onclick="Submit_Click("here I want to catch the attribute of another tag, not this one")">Save changes</button>

【问题讨论】:

  • “捕获”是什么意思?您始终可以通过getElementById()querySelector() 等定位其他元素。
  • 你能举例说明你想如何使用这段代码吗?

标签: javascript attributes getattribute


【解决方案1】:

您可以像这样使用 querySelector 或 getElementbyId:

<button onclick="Submit_Click(document.getElementById('id-of-other-element').getAttribute('id'));">Save changes</button>

【讨论】:

  • 这行得通。这是一个非常简单的解决方案,也是一个简单的解决方案。非常感谢!
【解决方案2】:

试试这个:

yourElement.onclick = function() {
    var otherID = document.getElementById('youOtherElementId').id;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多