如何将 javascript 变量值设置为 html 元素的 id 属性 [英] How can set javascript variable value as id attribute of a html element

查看:33
本文介绍了如何将 javascript 变量值设置为 html 元素的 id 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 HTML 页面中有以下 JavaScript

Say I have the following JavaScript in a HTML page

 <html>
 <script>
 var i=0;
 function data()
      {
        i++;
        var id="form"+i;
       }

   </script>
  <body>
  <a href="#x" onclick="data();"</a>
  </body>
  </html>

如果有标签,我如何获取变量 id 的值作为 id.请任何人帮助我

How do I get the value of the variable id as the id if the tag.please anyone help me

推荐答案

试试这个:

<html>
 <script>
 var i=0;
 function data(ele)
      {
        i++;
        ele.setAttribute("id","form"+i);
       }

   </script>
  <body>
  <a href="#x" onclick="data(this);"</a>
  </body>
  </html>

这篇关于如何将 javascript 变量值设置为 html 元素的 id 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆