dfe*_*aro -3 javascript jquery
我在页面上有几个锚标签,其id为'hrefCompare'.我需要在所有这些标签上动态设置href属性的值.
我目前正在尝试这样做:
$("#hrefCompare").attr("href", "foobar.com");
Run Code Online (Sandbox Code Playgroud)
但是,这仅设置具有该ID的第一个锚标记.此页面上还有7个具有相同ID"hrefCompare".如何使用该ID设置所有href值?
id必须是唯一的,在这种情况下我建议你使用class,它应该完美无缺:
$(".hrefCompare").attr("href", "foobar.com");
<a href="#" class="hrefCompare">a</b>
<a href="#" class="hrefCompare">b</b>
Run Code Online (Sandbox Code Playgroud)