如果我知道按钮的 id,如何使用 javascript 设置 DOM 按钮的标题

How can I set the title of a DOM button using javascript if I know the id of the button?

本文关键字:按钮 javascript 设置 DOM 何使用 标题 我知道 id 如果      更新时间:2023-09-26

我有一个按钮:

<button id="abc">abc</button>

如何设置此按钮的标题属性?

首先,获取对元素的引用:

var yourBtn = document.getElementById('abc');

然后,设置其 title 属性:

yourBtn.title = 'Something';
您可以使用

setAttribute方法

document.getElementById('abc').setAttribute("title", "Your title");