将 div id 更改为 javascript 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5805481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:58:13  来源:igfitidea点击:

Change div id into javascript variable

javascripthtml

提问by steven22

Ok so now I am using the code below which works fine. The selected option is in the variable:'val' . but now I want to change the div id. So how do I change the value of 'var_div' into the value of 'val'?

好的,现在我正在使用下面的代码,它可以正常工作。所选选项位于变量中:'val'。但现在我想更改 div id。那么如何将“var_div”的值更改为“val”的值?

 <form name="form1" method="POST">
    <select name="select1" onchange="updatevariable(this)">
    <option value="div2" >2</option>
    <option value="div15" >15</option>
    </select>
    </form>

    <script type="text/javascript">
    function updatevariable(elm) {
    val = elm.options[elm.selectedIndex].value;
    window.alert(val);
    }
    </script>

    <div id='var_div'>
    </div>

回答by Hristo

How about this...

这个怎么样...

document.getElementById("var_div").setAttribute("id", val);

... with jQuery...

... 使用jQuery...

$('#var_div').attr('id',val);

I hope this helps.
Hristo

我希望这有帮助。
赫里斯托

回答by Joshua Carmody

document.getElementById("var_div").id = val;

Edit:

编辑:

By the way, you'll want to store the id of the element somewhere else so that you'll know what id to look for the next time you want to change it. If you don't, this code will only work once. If you want to support changing the ID repeatedly, you might want to do something like this:

顺便说一下,您需要将元素的 id 存储在其他地方,以便您知道下次要更改它时要查找的 id。如果不这样做,此代码将只工作一次。如果您想支持重复更改 ID,您可能需要执行以下操作:

<script type="text/javascript">
    var currentDivId = "var_div";

    function updatevariable(elm) {
        val = elm.options[elm.selectedIndex].value;
        window.alert(val);
        var divElement = document.getElementById(currentDivId);
        divElement.id = val;
        currentDivId = val;
    }
</script>

回答by Dve

You can do something as simple as

你可以做一些简单的事情

document.getElementById('var_div').setAttribute('id',val)

But you may also want to look into a framework such as jquery. Which, if you are going to be doing a lot stuff like this, is going to make your life a lot easier!

但您可能还想研究一个框架,例如 jquery。如果你要做很多这样的事情,会让你的生活变得更轻松!

回答by James

You can do that, if you need to:

如果您需要,您可以这样做:

<div id="test"></div>
<script type="text/javascript">
var elem = document.getElementById("test");
alert(elem.id);
elem.id = "test2";
alert(document.getElementById("test2").id);
</script>

Change the id property of the element after you have selected it using Javascript.

使用 Javascript 选择元素后,更改元素的 id 属性。

回答by Marwan

if this div is the only one in the form as show so its easy like this

如果这个 div 是表单中唯一的一个,那么就像这样简单

 <form name="form1" method="POST">
    <select name="select1" onchange="updatevariable(this)">
    <option value="div2" >2</option>
    <option value="div15" >15</option>
    </select>
    </form>

    <script type="text/javascript">
    function updatevariable(elm) {
    document.getElementByTagNames('DIV')[0].id= elm.options[elm.selectedIndex].value;
    }
    </script>

    <div id='var_div'>
    </div>

i did dynamicly get the div not by id because the id will be change by the change of the select tag and then change the id as the value of the option

我没有通过 id 动态获取 div,因为 id 将通过 select 标签的更改而更改,然后将 id 更改为选项的值

but if there is other divs in the form so we need to set an attribute to this div just like

但是如果表单中有其他 div,那么我们需要为这个 div 设置一个属性,就像

  <div id='var_div' changeId="true">
    </div>

and then loop over the divs and pic it up and change the id

然后循环遍历 div 并将其显示出来并更改 id

function updatevariable(elm) {
var divs =   document.getElementByTagNames('DIV');
   for(var i =0;i<divs.length;i++)
     {
      if(divs[i].getAttribute('changeId')!=null) {
        if(divs[i].getAttribute('changeId')=="true"){
              divs[i].id=elm.options[elm.selectedIndex].value;
             }
           }
      }
  }

that said Regards Please mark this answer as correct one if it ben useful to you :)

表示问候 如果对您有用,请将此答案标记为正确答案:)