简体   繁体   English

如何使用 Javascript 添加 Id 属性?

[英]How do I Add Id Attribute Using Javascript?

Can Anyone here could help me to solve my problem.这里的任何人都可以帮助我解决我的问题。 I have been trying to create Id of a button when it is clicked.我一直在尝试在单击按钮时创建按钮的 ID。 I have multiple button in my page and I want to and id attribute when it is clicked but note that this button had no id before it is clicked.我的页面中有多个按钮,我想在单击它时添加 id 属性,但请注意,在单击此按钮之前没有 id。 Here is some of my code.这是我的一些代码。

<div class="DivButton" style="" onclick="divClick()"></div>
<div class="DivButton" style="" onclick="divClick()"></div>
<div class="DivButton" style="" onclick="divClick()"></div>

This is my Html code这是我的 Html 代码

function divClick()
{
    $(this).attr("id") =="Active";     
}

Here is my JavaScript function but this does not worked as expectation.这是我的 JavaScript function 但这并没有达到预期。

You can pass this into divClick function.您可以将this传递给divClick function。

 function divClick(e){ e.id = "Active"; console.log(e); }
 <div class="DivButton" style="" onclick="divClick(this)">a</div> <div class="DivButton" style="" onclick="divClick(this)">b</div> <div class="DivButton" style="" onclick="divClick(this)">c</div>

let btn = document.querySelector('DivButton'); btn.setAttribute("id","Active");

It is generally not a good idea to use inline event handlers.使用内联事件处理程序通常不是一个好主意

If you only add an id to elements, you'll end up with duplicate id's, which is not allowed in html.如果你只给元素添加一个 id,你最终会得到重复的 id,这在 html 中是不允许的。 Better use an.active css-class.最好使用 an.active css-class。

The snippet uses event delegation .该片段使用事件委托

 document.addEventListener("click", handle); function handle(evt) { if (evt.target.classList.contains("DivButton")) { document.querySelectorAll(".DivButton").forEach( bttn => bttn.classList.remove("active") ); return evt.target.classList.add("active"); } }
 .DivButton { cursor: pointer; width: 125px;l height: 2rem; line-height: 2rem; text-align: center; border: 1px solid #999; margin: 0.3rem; }.active { color: red; background-color: #FFFFC0; }
 <div class="DivButton active">a</div> <div class="DivButton">b</div> <div class="DivButton">c</div>

Try this html code试试这个 html 代码

<div class="DivButton" style="" onclick="divClick(this)">Button1</div>
<div class="DivButton" style="" onclick="divClick(this)">Button2</div>
<div class="DivButton" style="" onclick="divClick(this)">Button3</div>

Javascript code id should be unique Javascript 代码 id 应该是唯一的

<script>
function divClick(e){
 e.id = Math.floor(Math.random() * 10);   
 }
</script>

You should probably check if the id active exists and remove it if it does so it can be added to the new button.您可能应该检查id active 是否存在,如果存在则将其删除,这样可以将其添加到新按钮中。

 function divClick(e,idToAdd){ if(document.querySelector('#active')){ document.querySelector('#active').removeAttribute('id'); } e.id = 'active'; }
 .DivButton { display: inline-block; margin: 1rem; padding: .5rem 3rem; background: #000; color: #fff; cursor: pointer; } #active { background: red; }
 <div class="DivButton" style="" onclick="divClick(this)">a</div> <div class="DivButton" style="" onclick="divClick(this)">b</div> <div class="DivButton" style="" onclick="divClick(this)">c</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM