我想在单击按钮后使用id在我的html文件中获取两个<p>
元素的名称。但在我的代码中,当我点击按钮时,它只输出“打印”。我怎样才能做到这一点?如何在JavaScript中使用ID获取值多个标签?
这是我的HTML代码:
<p id="name" name="print">hey</p>
<p id="name" name="this">hey</p>
<button id="btn11">test11</button>
这是我的javascript代码:
$("#btn11").click(function(){
alert($("#name").attr("name"));
});
Id在HTML中是唯一的 –
使用'class'而不是'id' – Sapikelio